How to add attributes to product grid in category
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:
- Ensure your attribute appears in the Magento backend, is assigned to the appropriate attribute sets, and is set for applicable products
- Disregard the Update layout XML section
- 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:
- //...
- /**
- * 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;
- }
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)
- <layout>
- <!-- ... -->
- <catalog_category_default>
- <!-- ... -->
- <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
- <!-- START UPDATE -->
- <action method="addAttribute"><name>MyAttribute</name></action>
- <action method="addAttribute"><name>AnotherCustomAttribute</name></action>
- <!-- END UPDATE -->
- </block>
- <!-- ... -->
- </catalog_category_default>
- <!-- ... -->
- </layout>
Do same thing for <catalog_category_layered>
Update templates |
app/design/frontend/default/default/template/catalog/product/list.phtml:
- <!-- ... -->
- <?php foreach ($_productCollection as $_product): ?>
- <!-- ... -->
- <!-- these are regular text attributes -->
- <?php echo $_product->getMyAttribute() ?>
- <?php echo $_product->getAnotherCustomAttribute() ?>
- <!-- this is dropdown attribute -->
- <?php echo $_product->getAttributeText('your_attribute') ?>
- <!-- this is a boolean attribute -->
- <?php echo $_product->getMyAttributeName() ? "Yes" : "No" ?>
- <!-- ... -->
- <?php endforeach?>
- <!-- ... -->





