Add custom attributes to Magento product grid or listing page

Saturday, 14. April 2012

Thought this will help someone. If you want to use a custom attribute in category product listing pages follow these steps below:

1) Ensure addAttribute Method exists
Navigate to app/code/core/Mage/Catalog/Block/Product/List.php. Search for a method called addAttribute

This should already been added with new versions of Magento, but if your version doesn’t have it. Copy this file to

app/code/local/Mage/Catalog/Block/Product/List.php

Then before the end of class add this script:

  /**
   * Use this method in layouts for extra attributes
   *
   * @param string $code internal name of attribute
   */
  public function addAttribute($code)
  {
    $this->_getProductCollection()->addAttributeToSelect($code);
    return $this;
  }

2) Update your catalog.xml file
Once we have the method in place. Now navigate to your theme’s catalog.xml file and open it.
– Look for handlers catalog_category_default and catalog_category_layered
– then add your attribute codes as show below:

<catalog_category_default>
    <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">			
	...
	<action method="addAttribute"><name>attribute_code</name></action>
	<action method="addAttribute"><name>attribute_code2</name></action>
	...			
   </block>
</catalog_category_default>

3) Use your attribute in list.phtml page
Now you can use the attribute in your theme’s list.phtml page just as normal.

    echo $_product->getAttributeCode(); # OR
    echo $_product->getData('attribute_code2'); # OR
    echo $_product->getAttributeText('attribute_code2');

Enjoy!

Leave a Reply

You must be logged in to post a comment.