Add custom attributes in product grid or listing page in Magento



Sometimes we want to add the the New/Custom products attributes and wants to display this attributes on the frontend products listing page. It is very easy and can done by using the steps given below:

Step 1. Navigate to app/code/core/Mage/Catalog/Block/Product/List.php, and copy this file to app/code/local/Mage/Catalog/Block/Product/List.php, so that you will be safe for the Magento version upgrade.

Step 2. Search for the addAttribute methods and Insert the code below before the end of class

/**
* 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;
}

Note: This code is already added with new versions of Magento, but if you are using the older version then add the code chunk.

Step 3. Navigate to the app/design/frontend/default/your_custom_theme/layout/ and open the catalog.xml file or you can find this file by navigating to the app/design/frontend/base/default/layout/ and open it in the editor of your choice. Search for handlers catalog_category_default and catalog_category_layered and add the New/Custom attribute code

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

Step 4. Use your attribute in list.phtml page
Now you can use the custom attribute in your themes list.phtml page just as normal, by adding using the below syntax

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

You have done and refresh your page to view the changes.