I wanted to add custom column called brands(dropdown) in magento product grid in backend.I achieved this task but the issue is when i start filter the brands the column brands disappers and when i refresh the page then comes back.can any one please help me. Here is my code.
in _prepareCollection() function i added
$collection = Mage::getModel('catalog/product')->getCollection()
 ->addAttributeToSelect('sku')
 ->addAttributeToSelect('name')
 ->addAttributeToSelect('attribute_set_id')
 ->addAttributeToSelect('type_id')
 ->addAttributeToSelect('brands'); // added by me
in _prepareColumns()
$brands = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id','attribute_code');
 foreach($brands as $brand):
 if($brand->getAttributeCode() == 'brands')
 $brand_options[$brand->getOptionId()] = $brand->getValue();
 endforeach;
 $this->addColumn('brands',
 array(
 'header'=> Mage::helper('catalog')->__('Brands'),
 'width' => '10px',
 'index' => 'brands',
 'type' => 'options',
 'sortable' => false,
 'options' => $brand_options,
 ));
 1 Answer 1
First add the add new function on grid.php getAttributeOptions
 protected function _getAttributeOptions($attribute_code)
 {
 $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
 $options = array();
 foreach( $attribute->getSource()->getAllOptions(false, true) as $option ) {
 $options[$option['value']] = $option['label'];
 }
 return $options;
 }
then add modify
$this->addColumn('brands', array(
 'header' => Mage::helper('catalog')->__('Brand'),
 'align' => 'left',
 'width' => '80px',
 'index' => 'brands',
'final_index' =>'brands',
 'type' => 'options',
 'options' => $this->_getAttributeOptions('brands'),
 ));
 - 
 i had used your code and still the same issue.when i filter brands by any brand name the the column brands dissapers and it will come back when i refresh the page.Pradeep Sanku– Pradeep Sanku2014年02月03日 10:10:43 +00:00Commented Feb 3, 2014 at 10:10
 - 
 please check... final add 'final_index' =>'brands'>also enable brands attribute in product listing from Manage Attribute>USed in Product listing2014年02月03日 10:21:02 +00:00Commented Feb 3, 2014 at 10:21
 - 
 still the same issue.i had checked the product listing is selected to Yes in product listing page.and after adding final_index also not working.Pradeep Sanku– Pradeep Sanku2014年02月03日 10:28:59 +00:00Commented Feb 3, 2014 at 10:28
 - 
 1Pradeep ,this code is working on my system...2014年02月03日 10:56:44 +00:00Commented Feb 3, 2014 at 10:56
 - 
 oh.may be it will some other issue on my system.need to debug.thanks a lot.Pradeep Sanku– Pradeep Sanku2014年02月03日 11:13:08 +00:00Commented Feb 3, 2014 at 11:13