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

This entry was posted in Magento on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

2 thoughts on “Magento – Display Custom Option Quantity in Manage Product Grid”

  1. Anonymous

    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

    Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *