Monday, December 10, 2012

Magento - Display Custom Option Quantity in Manage Product Grid

Hello,

I am back to blogging after long time. Past few months were really busy for me so did not get enough time for blogging.  So recently I was working on magento project, where we have used MageWrox Advanced Custom Option extension to have quantity and SKU for each custom option.

When you go to Catalog --> Manage Products, in grid only total qty is displayed. Our requirement was to display custom options and it's qty there. So how to do that? Here is the trick.

Copy following file


app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

Now create following folder structure in side app/code/local 

Mage -->
          Adminhtml -->
                              Block -->
                                         Catalog -->
                                                       Product

And put grid.php file there. Now open grid.php file and find following code.

if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
            $this->addColumn('qty',
                array(
                    'header'=> Mage::helper('catalog')->__('Qty'),
                    'width' => '100px',
                    'type'  => 'number',
                    'index' => 'qty',
            ));
        }

This is the qty column which displays total inventory quantity. We don't want this so remove it and put following code instead of it.

$this->addColumn('customqty',
            array(
                'header'=> Mage::helper('catalog')->__('Qty'),
                'width' => '200px',
                'index' => 'entity_id',
            'type' => 'text',
            'renderer' =>  'Mage_Adminhtml_Block_Catalog_Product_Renderer_CO'
        ));

Here we will use renderer to format the data we have to display. Now created Renderer folder in 

app/code/local/Mage/Adminhtml/Block/Catalog/Product/

and create co.php file there and add following code there.


class Mage_Adminhtml_Block_Catalog_Product_Renderer_CO extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value =  $row->getData($this->getColumn()->getIndex());
$product = Mage::getModel('catalog/product');
$product->load($value);
$customOptionHTML = '';
foreach ($product->getOptions() as $opt) {
$values = $opt->getValues();
foreach($values as $v){
$customOptionHTML.=$v['default_title'].' : '.$v['customoptions_qty'].'
';
}
}
return ''.$customOptionHTML.'';
}
}

?>
Here we are using product id to get product model and it's custom options. foreach loop is used to iterate through custom options and get custom option title and qty. Here you can make any change in format if you want. 

Please note that this code is tested in magento community version 1.7.0.2








3 comments:

  1. NICE one , I want to add qty in custom fields to show price as per qty in custm fields like

    white tshirt $5 1 piece, color tshirt $6 for 1 piece

    white tshirt $4 for 12 piece , color tshirt $5 for 12 or above pieces

    Would you please help me to sort out this

    thanks

    ReplyDelete
  2. Hi, Do U know how thah extension connects template options to the product?

    ReplyDelete
  3. I try to add this module but it is not shown up in custom option

    ReplyDelete