How to add attributes to product grid in category

Last modified by scorxn on Thu, June 24, 2010 16:40
Source|Old Revisions  

This is an old revision of the document!


Introduction

Here we will show how to add your more attributes to the category view product list or grid.

Magento 1.2+ Errors

Should the instructions below produce an error like Invalid method Mage_Some_Object::addAttribute under Magento 1.2+, try the following:

  1. Ensure your attribute appears in the Magento backend, is assigned to the appropriate attribute sets, and is set for applicable products
  2. Disregard the Update layout XML section
  3. Follow the Update templates instructions below

Update catalog/product_list block

NOTE: Since v0.9 this method is already implemented, 1.1 supports the XML method.

Add this method to Mage_Catalog_Block_Product_List: app/code/core/Mage/Catalog/Block/Product/List.php:

  1. //...
  2.   /**
  3.    * Use this method in layouts for extra attributes
  4.    *
  5.    * @param string $code internal name of attribute
  6.    */
  7.   public function addAttribute($code)
  8.   {
  9.     $this->_getProductCollection()->addAttributeToSelect($code);
  10.     return $this;
  11.   }

Update layout XML

This method is available in 1.1

Update layouts where needed, add your custom attributes you need to show:

  1. <layout>
  2. <!-- ... -->
  3.   <catalog_category_default>
  4. <!-- ... -->
  5.       <block type="catalog/product_list" name="product_list">
  6. <!-- START UPDATE -->
  7.         <action method="addAttribute"><attribute>MyAttribute</attribute></action>
  8.         <action method="addAttribute"><attribute>AnotherCustomAttribute</attribute></action>
  9. <!-- END UPDATE -->
  10.       </block>
  11. <!-- ... -->
  12.   </catalog_category_default>
  13. <!-- ... -->
  14. </layout>

Do same thing for <catalog_category_layered>

Update templates

app/design/frontend/default/default/template/catalog/product/list.phtml:

  1. <!-- ... -->
  2. <?php foreach ($_productCollection as $_product):?>
  3. <!-- ... -->
  4. <!-- these are regular text attributes -->
  5.   <?php echo $_product->getMyAttribute()?><br />
  6.   <?php echo $_product->getAnotherCustomAttribute()?>
  7.  
  8. <!-- this is dropdown attribute -->
  9.   <?php echo $_product->getAttributeText('your_attribute')?>
  10.  
  11. <!-- this is a boolean attribute -->
  12.   <?php echo str_replace(1,"Yes",str_replace(0,"No",$_product->getMyAttributeName()))?>
  13. <!-- ... -->
  14. <?php endforeach?>
  15. <!-- ... -->



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs