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,
        ));
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'),
        ));
  • 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 Feb 3 '14 at 10:10
  • please check... final add 'final_index' =>'brands'>also enable brands attribute in product listing from Manage Attribute>USed in Product listing – Amit Bera Feb 3 '14 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 Feb 3 '14 at 10:28
  • 1
    Pradeep ,this code is working on my system... – Amit Bera Feb 3 '14 at 10:56
  • oh.may be it will some other issue on my system.need to debug.thanks a lot. – Pradeep Sanku Feb 3 '14 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.