050 5029525 info@impulsmedia.nl
Selecteer een pagina

Magento: Add attribute columns in ‘Manage Products’ grid

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,
));
->addAttributeToSelect('supplier')

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

 

http://www.leonhostetler.com/blog/magento-add-attribute-columns-in-manage-products-grid-201205/