Author Topic: Hack: SKU in Related Products (backend)  (Read 6297 times)

Daniel Jonsson

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 109
Hack: SKU in Related Products (backend)
« on: November 13, 2008, 11:22:35 am »
This hack shows SKU in the Related Products tab under Products in the backend system.

If you have a large database of articles it's usually impossible to keep track of all items, specially if they have similar names, so SKU is instead used. This hack makes it possible to use SKU also in the related products search function.

/administrator/components/com_virtuemart/html/product.product_form.php
Under head comments add:
Code: [Select]
/* SKU MOD
* Modified AJAX search and listing for sku.
* Copyright (C) 2008 Xclude - All rights reserved.
* Author Daniel Jonsson
* http://www.xlude.se
*/


Between
Code: [Select]
foreach( $related_products as $relProd ) {and
Code: [Select]
}Replace the single line with:
Code: [Select]
/* START SKU MOD */
 $relProducts[$relProd] = $ps_product->get_field( $relProd, 'product_sku'). ", " .$ps_product->get_field( $relProd, 'product_name');
/* END SKU MOD */

Between
Code: [Select]
?>
var productSearchField = function(){
and
Code: [Select]
relatedSelection = document.getElementById('relatedSelection');
    related_products = document.adminForm.related_products;

Change all the lines to:
Code: [Select]
<?PHP /* START SKU MOD */ ?>
    var relds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'index2.php?option=com_virtuemart&page=product.ajax_tools&task=getproducts&ajax_request=1&func=&no_menu=1&only_page=1&no_html=1&product_id=<?php echo $product_id ?>',
            method: 'GET' }),
        reader: new Ext.data.JsonReader({
            root: 'products',
            totalProperty: 'totalCount',
            id: 'product_id'
        }, [
            {name: 'product'},
            {name: 'category'},
            {name: 'product_id'},
                   {name: 'product_sku'}
        ])
    });
 
    // Custom rendering Template
    var resultTpl = new Ext.XTemplate( '<tpl for="."><div class="x-combo-list-item">{product_sku}, {product} / {category}</div></tpl>' );
<?PHP /* END SKU MOD */ ?>

/administrator/components/com_virtuemart/html/product.ajax_tools.php
Under head comments add:
Code: [Select]
/* SKU MOD
* Modified AJAX search and listing for sku.
* Copyright (C) 2008 Xclude - All rights reserved.
* Author Daniel Jonsson
* http://www.xlude.se
*/


Between
Code: [Select]
case 'getproducts':and
Code: [Select]
case 'getcategories':Change to
Code: [Select]
if(!defined('SERVICES_JSON_SLICE'))
require_once(CLASSPATH . 'JSON.php');
$db =& new ps_DB;
$keyword = $db->getEscaped(vmGet( $_REQUEST, 'query' ));
/* START SKU MOD */
$q = "SELECT SQL_CALC_FOUND_ROWS #__{vm}_product.product_id,category_name,product_name, product_sku
FROM #__{vm}_product,#__{vm}_product_category_xref,#__{vm}_category ";
/* END SKU MOD */
if( empty($_REQUEST['show_items']) ) {
$q .= "WHERE product_parent_id='0'
AND #__{vm}_product.product_id <> '$product_id'
AND #__{vm}_product.product_id=#__{vm}_product_category_xref.product_id
AND #__{vm}_product_category_xref.category_id=#__{vm}_category.category_id";
}
else {
$q .= "WHERE #__{vm}_product.product_id <> '$product_id'
AND  #__{vm}_product.product_id=#__{vm}_product_category_xref.product_id
AND #__{vm}_product_category_xref.category_id=#__{vm}_category.category_id";
}
if( $keyword ) {
$q .= ' AND (product_name LIKE \'%'.$keyword.'%\'';
$q .= ' OR category_name LIKE \'%'.$keyword.'%\'';
/* START SKU MOD */
$q .= ' OR product_sku LIKE \'%'.$keyword.'%\')';
/* END SKU MOD */
}
$q .= ' ORDER BY category_name,#__{vm}_category.category_id,product_name';
$q .= ' LIMIT '.(int)$_REQUEST['start'].', '.(int)$_REQUEST['limit'];
$db->query( $q );

while( $db->next_record() ) {
/* START SKU MOD */
$response['products'][] = array( 'product_id' => $db->f("product_id"),
'category' => htmlspecialchars($db->f("category_name")),
'product' => htmlspecialchars($db->f("product_name")),
'product_sku' => htmlspecialchars($db->f("product_sku"))
);
/* END SKU MOD */
}
$db->query('SELECT FOUND_ROWS() as num_rows');
$db->next_record();
$response['totalCount'] = $db->f('num_rows');
error_reporting(0);
while( @ob_end_clean() );
$json = new Services_JSON();
echo $json->encode( $response );
$vm_mainframe->close(true);

break;


That is it, I think it should be included into VM, I will add it into Shopper Group Extension anyway.
/Best wishes, Daniel
Donations are appreciated.

jolival

  • Beginner
  • *
  • Posts: 30
  • www.redespt.com
    • www.redespt.com
Re: Hack: SKU in Related Products (backend)
« Reply #1 on: November 17, 2008, 23:42:28 pm »
tested on J1.5.8 and VM 1.1.2,

Great Hack.

Daniel Jonsson

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 109
Re: Hack: SKU in Related Products (backend)
« Reply #2 on: November 18, 2008, 12:50:26 pm »
Thanks...
Donations are appreciated.

jolival

  • Beginner
  • *
  • Posts: 30
  • www.redespt.com
    • www.redespt.com
Re: Hack: SKU in Related Products (backend)
« Reply #3 on: November 18, 2008, 16:17:13 pm »
Hello Daniel

Do you now how to increase the search box?

My relatedsearchbox have 262px and i want to put wigth: 320px but i donĀ“t now where...

Tks in advance
jolival


Daniel Jonsson

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 109
Re: Hack: SKU in Related Products (backend)
« Reply #4 on: November 19, 2008, 06:22:45 am »
In product.product_form.php:

Search for:
Code: [Select]
        loadingText: '<?php echo addslashes($VM_LANG->_('VM_PRODUCT_SEARCHING')); ?>',
        width: 170,
        minListWidth: 170,

Change the value width and minListWidth. I recommend 400.

/Best wishes, Daniel
Donations are appreciated.

jolival

  • Beginner
  • *
  • Posts: 30
  • www.redespt.com
    • www.redespt.com
Re: Hack: SKU in Related Products (backend)
« Reply #5 on: November 19, 2008, 11:29:40 am »
tks Daniel, works perfectly

jolival
RedesPT

zeevee

  • Beginner
  • *
  • Posts: 25
    • Euphoric Graphics
Re: Hack: SKU in Related Products (backend)
« Reply #6 on: November 24, 2008, 16:08:18 pm »
Big Thank you!!!!

Gadensgaard

  • Guest
Re: Hack: SKU in Related Products (backend)
« Reply #7 on: August 22, 2009, 12:11:20 pm »
Just what i needed! You just saved my several hours of guessing from similar named products. Thanks!