Magento

eCommerce Software for Online Growth

How to add attributes to product grid in category

Last modified by canario_aleman on Sun, January 17, 2010 05:20
Source|Old Revisions  

Introduction

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

*Be sure that each attribute you want to add to the product grid is set to be “Used in product listing” or nothing will show up!*

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

NOTE: Adding your custom attribute in catalog.xml is obsolete for version 1.3.X (Community Edition) and 1.6.0 (Enterprise Edition).

Update layouts where needed, add your custom attributes you need to show: (Morningtime: you need to edit the file in .../layout/catalog.xml)

  1. <layout>
  2. <!-- ... -->
  3.   <catalog_category_default>
  4. <!-- ... -->
  5.       <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
  6. <!-- START UPDATE -->
  7.         <action method="addAttribute"><name>MyAttribute</name></action>
  8.         <action method="addAttribute"><name>AnotherCustomAttribute</name></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() ?>
  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 $_product->getMyAttributeName() ? "Yes" : "No" ?>
  13. <!-- ... -->
  14. <?php endforeach?>
  15. <!-- ... -->



 

Magento User Guide

Magento Enterprise Available now

Professional Services from the Magento Team

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs
© Copyright 2010 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
177522 users|1153 users currently online|277044 forum posts