Magento: Add attribute columns in ‘Manage Products’ grid

May, 11, 2012

Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.

The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php

Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;


$this->addColumn('sku',
	array(
		'header'=> Mage::helper('catalog')->__('SKU'),
        'width' => '80px',
        'index' => 'sku',
    ));

That’s the code that adds the SKU column to the product grid.  Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().


$this->addColumn('supplier_id',
	array(
		'header'=> Mage::helper('catalog')->__('Supplier ID'),
		'width' => '150px',
		'index' => 'supplier_id',
	));

Then add the following line to _prepareCollection() where the other attributes are listed like this;


->addAttributeToSelect('supplier_id')

That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.

The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.

Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():


        $supplier_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');
        foreach ($supplier_items as $supplier_item) :
            if ($supplier_item->getAttributeCode() == 'supplier')
                $supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue();
        endforeach;

        $this->addColumn('supplier',
            array(
                'header'=> Mage::helper('catalog')->__('supplier'),
                'width' => '150px',
                'type'  => 'options',
                'index' => 'supplier',
                'options' => $supplier_options,
        ));

And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;


->addAttributeToSelect('supplier')

That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.

Posted in Web Development | Tagged as: | 11 Comments

11 Responses to Magento: Add attribute columns in ‘Manage Products’ grid

  1. Mondane says:

    NB Don’t copy core files, but use observers and rewrites to accomplish your task. This way, when you upgrade Magento, new functionality will work.

    For adding a column the proper way, look here: http://stackoverflow.com/a/5994209/1025437

    • Mondane says:

      I added another solution to stackoverflow using just rewrites. The used observer events are, in my opinion, to global.

  2. joeymetro says:

    wow, simply amazing. working from CE 1.6.2 and works perfectly!

  3. Phillip says:

    Super simple and effective, fantastic, thanks!
    (worked on 1.7)

  4. Test says:

    Thanks for sharing.. working with this solution…

  5. theobaldus says:

    With this solution when I search products by name option of supplier disappears. Other filters works great.

  6. makers says:

    It’s work bad with me 🙁 .
    SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘custom_params’ cannot be null
    🙁 🙁

  7. Chris says:

    Very strange method having the if statement inside the foreach, when you could just addFieldToFilter(‘attribute.attribute_code’, ‘supplier’) instead – which is better in every possible way

    • leonh says:

      I am not a Magento (or PHP) expert. I just cobbled together code that did what I needed it to do. I’m sure there are better ways of doing it.

  8. Paul says:

    I have seen this on in other places and it works great. Where can I find a list of types? I have a Yes/No attribute that I wish to display in grid view but it is only showing 0 or 1. I have tried ‘options’, ‘select’, ‘boolean’ and a few other guesses but none make it display as Yes or No

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: