Display custom option quantity in manage product grid in magento

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

Display custom option quantity in manage product grid in magento

Display custom option quantity in manage product grid in magento

Here, we are going to write article about display custom option quantity in manage product 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.

1
2
3
4
5
6
7
8
9
10
 
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.

1
2
3
4
5
6
7
8
9
 
$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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
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

Add comment


 

Pin It on Pinterest

Share This