3

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,
 ));
Murtuza Zabuawala
14.8k10 gold badges47 silver badges76 bronze badges
asked Feb 2, 2014 at 19:37

1 Answer 1

10

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'),
 ));
Fra
6,98512 gold badges66 silver badges101 bronze badges
answered Feb 2, 2014 at 20:52
5
  • 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. Commented 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 listing Commented 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. Commented Feb 3, 2014 at 10:28
  • 1
    Pradeep ,this code is working on my system... Commented Feb 3, 2014 at 10:56
  • oh.may be it will some other issue on my system.need to debug.thanks a lot. Commented Feb 3, 2014 at 11:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.