Magento: Displaying Custom Attributes on Product View

Adding custom attributes to your product view is very easy. Adding them to the product listing (category pages) is much more difficult. You’ll need to create a custom module for that. In this post though, I’ll only be showing you how to add attributes to the product view page.

If it is a text field (textbox or textarea), this is all you have to do:

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

There is one thing different here though. If your Attribute Code is “shirt_size”, then you would use getShirtSize(). Remove the underscores and capitalize the first letter of each word. It is picky that way. if you use getshirtsize(), it won’t work.

If you are using a dropdown or a multiple select, you have to call it a little differently:

1
<?php echo $_product->getAttributeText('shirt_size') ?>

This method requires the actual Attribute Code. If you are displaying the value from a dropdown, you’ll get exactly what you need with this call. If you are wanting to display the values from a multiple select, it will return an array.

This entry was posted in Magento. Bookmark the permalink.

48 Responses to Magento: Displaying Custom Attributes on Product View

  1. Astronomad says:

    What file is this in?

  2. Josh Pratt says:

    Astronomad,

    /app/design/frontend/default/default/template/catalog/product/view.phtml

  3. Bivek says:

    I need to show select box of my new attributes(color/size). that i did by using above method.

    Now i need to show the attribute in shopping cart and also it must be shown in order details page how can i do that?

  4. Monkeyclaus says:

    Thanks so much for this great info. I used this to show an attribute on view.phtml that is a link. I see the link and my attribute but it is text rather than an active hyperlink. Any ideas ? Thanks for the wonderful resource Josh !

  5. Srdjan says:

    Thanks.. I was wondering why my getAttributeText wasn’t working for the text field.

  6. Jaime says:

    and how for price fields?

  7. spuff says:

    Fantastic, thank you soooo much! How do people find out this stuff?

  8. Jefte says:

    Thank you for this little snippet. It was exactly what I was looking for. Is there a list of common functions for Magento like can be found in the WordPress codex? I find the Magento wiki to be… lacking.

  9. Bryan says:

    thank you soo much, this is exactly what I was looking for.

  10. ted says:

    using magento 1.3.2.4, how to get custom attributes in the /app/design/frontend/default/default/template/catalog/product/list.phtml intead of view.phtml.
    i want to show custom attributes in the product listing when people are searching for it.
    i tried using $_product->getNextShipmentDate() but it returns null in list.phtml. it returns the expected value in view.phtml.

  11. ted says:

    i got it.. i had to enable ‘Used in product listing’ in the attribute settings.
    after enabling this, $_product->getNextShipmentDate() is not returning null anymore and showing the same value as in view.pthml.
    now i need to figure out how they format the date for display.

  12. Devin says:

    great for product pages… however getAttributeText(”) for custom category attributes.. not so good.

    The attribute I just created is a drop down for categories, now getNAMEOFATTRIBUTE() returns a 1,2 or 3 (the total number of drop down options I have) but never the value of the selected attribute option. getAttributeText(”) as you stated is for drop downs returns nothing.

    Completely opposite for product pages though.

  13. Harry says:

    Thanks “ted”.
    Information provided by you helped me a lot.
    Thanks again…

  14. Baldev says:

    Thank you so much for your valuable contribution

  15. henri says:

    i created an attribute which is price attribute and i named it “retailprice”. I can see it in the additional information tab. How can i show it in the product list?

  16. pankaj says:

    How to get the product cost attribute value on reports .

    i need this product cost feild to be displayed on sales report and best sellers report.
    report–>sales–> sales report
    report–>Advanced–> bestsellers

    plz help its urgent.

  17. Devin says:

    Any idea why this wont work on catelog->category->view.pthml when trying to call out category attributes? As I stated above, getAttributeText(‘my_attribute’) for category drop downs or multi-select wont work on category level..

    Very frustrating.

  18. Devin says:

    OH got it!

    Note to self (and others):

    $categoryFull = Mage::getModel(“catalog/category”)->load($this->getCurrentCategory()->getId());
    $attributes = $categoryFull->getAttributes();
    echo $attributes['your_custom_attribute']->getFrontend()->getValue($categoryFull);

    This will get values of custom attributes for categories!

  19. Dennis says:

    isnt this posible in the xml files? I dont like changing the templates…

  20. zdhkw says:

    It’s great! Thank you!

  21. Jeff says:

    muchos gracious, I’ve found this to be a very useful post!

  22. RBusiness says:

    I know this keeps getting rehashed but I’m not getting any results by using the code! I have everything set correctly from what I can tell.

    Modern Theme
    Magento 1.4.1.0
    Cache OFF
    Attribute is set to display on product page!

    The attribute actually shows up in the TAB where it should. But I can’t get the data to repeat. I’ve got php skills too! It’s burning me here…

    My attribute is “bsc_brand_nm” I’m using…

    getBscBrandNm() ?>

    Anyone see an error that my eyes are blurring on?

    • Josh Pratt says:

      RBusiness – it appears that your code didn’t fully come through. I’ll email you, and if you could send me a pastebin of the code and let me know what type of attribute it is, I’ll try to help.

  23. Bampnet says:

    Why difficult? For retrieve attributes from category page here is the code:
    $_product->getResource()->getAttribute('vehicle_type')->getFrontend()->getValue($_product)

    vehicle_type is the attribute code

  24. Gavin says:

    Thanks for providing this.
    I noticed that if I had an attribute with ‘multiple select’ and in the admin only one option was selected it didn’t display the array.

    I’ve expanded the code in that scenario: [EDIT: Sorry, the code didn't come through right...]

  25. Hans says:

    Thanks, very useful.
    But I’m trying to call a custom attribute in the head section of my product page, and that’s not working with getAttributeName() ?>

    Do you have an idea how to call an attribute in the head section?

    • Josh Pratt says:

      Hans – The block that serves up the head of your page does not have access to the current product object. You’ll notice at the top of the view.phtml template, there is this line: $_product = $this->getProduct();. $this refers to the block that is loading up all of the data for the view template. I’m not sure why you’d want to get at product attributes in the head template… But, one thing you could try is Mage::registry(‘current_product’)->getData(‘attribute_code’). I haven’t tested it, but that may work…

      • Hans says:

        Hi Josh, I did notice the line to load the product in view.phtml, so I allready tried putting that line in the head.phtml but that didn’t work either.

        So I just tested the Mage::registry(‘current_product’)->getData(‘attribute_code’) code and that worked great, thanks again for that one!

        Now I can put my own specified canonical link to the products pages.

  26. Some times like in grouped.phtml you have to do it like this
    htmlEscape($_item->getAttributeText(‘contenance’)); ?>

  27. omikron says:

    The attribute label doesn’t get translated when changing the store’s language.
    It’s interesting because the same attribute gets translated in the more info tab

    any ideas on how to solve this?

  28. this wasn’t working for me until I removed the ‘quotes’ around shirt_size. If you’re having trouble with this in magento 1.4+, that may be your issue.

  29. ravi says:

    Very good article

  30. How to display custom attrubutes in magento 1.4.1.1 ?

  31. Vishal Sanwar says:

    solution of call custom attribute

    function getAttr($product_id, $attributeName) {
    $product = Mage::getModel(‘catalog/product’)->load($product_id);
    $attributes = $product->getAttributes();
    $attributeValue = null;
    if(array_key_exists($attributeName , $attributes)){
    $attributesobj = $attributes["{$attributeName}"];
    $attributeValue = $attributesobj->getFrontend()->getValue($product);

    }
    return $attributeValue;

    }

  32. Ran says:

    Thanks Vishal. It works fine. catalog/output – to catalog/product do the trick…

  33. tuba says:

    Does anybody know how can I display attribute in the related product block?

  34. John Deszell says:

    Where do I add this snippet of code to display on product page? I’ve found numerous posts on other sites saying go to:

    /app/design/frontend/default/default/template/catalog/product/view.phtml

    But when I look at my server via FTP, I don’t have any other folders after template in that breadcrumb.

    Any suggestions? I’m using 1.6 CE

    Thanks!

  35. thank you very much.
    you saved me a lot of time. =)

  36. Sam says:

    My code looks the same; but on the front end I am seeing a number instead of the attribute; for example instead of Style: Garnet, It shows Style: 31.
    Any suggestions?

  37. Aditya Mohan says:

    Great work ! ! Thanks a lot !!

  38. Kalyan says:

    Hai friends i find this page as very useful.. but careful that themes also causing problem in displaying… if you are sure about the correctness of code but still not getting the output revert to the default theme and check it out…

    Thank u

  39. Biju says:

    absolutely the solution that i have been searching for since last few hours. thank you so much for this great piece of code!

  40. Peter says:

    Im using this code:
    htmlEscape($_product->getAttributeText(‘manufacturer’)); ?>

    but i want to add a link to it. so that when you clik on the attribute it opens a page showing all products with this attribute.

    Is that possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>