Displaying Magento custom product attributes on the frontend
12 Feb 2010, by Denis Margetic
5 Comments · Posted in Magento

    The simplest way to display custom attributes is to enable them on product view pages, where they normally show up as a table. Design files can be edited to display custom product attributes elsewhere on the product view page. When a Magento product object gets loaded in a template file, any custom attributes that have been added to products are also accessible. The method used to retrieve the values depends on the type of attribute. For drop-down attributes you can use the following code:

    <?php echo $_product->getAttributeText(’attribute_name’) ?>
    

    and for all other attribute types:

    <?php echo $_product->getAttributeName() ?>
    

    The above code will only return values on product view pages. If you would like to use these elsewhere, then you will need to make further modifications. Magento provides a backend setting to decide if the attribute is to be made available in product listings, such as category pages. Setting "Used in product listing" to yes makes the attribute values accessible on category and search pages.

    To access custom attributes on any other page requires modifications to the way the product collections are loaded. The functions responsible for loading products are:

    • public function getItemCollection()
    • public function getProductCollection()

    Which of the two is used will depend on the context. To load a custom attribute you need to modify the above functions to include an addAttributeToSelect('test_attribute') function call. Often Magento code already has addAttributeToSelect calls, hence searching for those calls in the relevant module is one way to find which function needs to be modified. For example, if you wanted to use a custom attribute, say recommended_retail_price, in the compare products sidebar, you would need to modify public function getItemCollection() in:

    
     app/code/core/Mage/Catalog/Helper/Product/Compare.php
    

    and change the following code:

    
    $this->_itemCollection->addAttributeToSelect('name')
        ->addUrlRewrite()
        ->load();
    

    to

    
    $this->_itemCollection->addAttributeToSelect('name')->addAttributeToSelect('recommended_retail_price')
        ->addUrlRewrite()
        ->load();
    

    Only then can you use <?php echo $_product->getRecommendedRetailPrice() ?> in the respective phtml template file to show the value on the compare sidebar.

    Equipped with this knowledge, it's easy to leverage flexibility of the product attribute system for extended functionality on the Magento frontend.

    Comments

    Thanks for this fantastic bit of information.

    One thing I would love to know is how to load the values of a multiple select (array)?

    Or simply to check if a value is selected in the array from the multiple select
    Comment by Anonymous - 15 Feb 2010 11:07:37 AM
    hello
    thanks for this tutorial
    how can I load a multiple select attribute?
    thanks
    Comment by choussamaster - 4 Jul 2010 9:30:00 PM
    Nice and really helpful, following code help me in 1.4.0.1 to show a custom attribute 'directions'

    [php]
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct();

    echo $_helper->productAttribute($_product, nl2br($_product->getDirections()), 'directions')
    [/php]
    Comment by tuba - 16 Jul 2010 4:09:00 AM
    This didn't work for me for a text field, but this did:

    [php]
    <?php echo $this->htmlEscape($_product->getData('attribute_code')); ?>
    [/php]

    Thanks,

    Tommy
    Comment by Tommy - 31 Jul 2010 7:27:00 PM
    Great and really helpful, following code help me a lot to show a custom attribute 'directions' thanks for sharing!
    Comment by rhizann2 - 9 Sep 2010 11:24:00 AM
    Comments are closed for this post