Magento – How to show custom options quantity in product management grid
This tutorial will show you how to show custom options quantity in product management grid in Magento
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 I am 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.