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.

21 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!

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> <pre lang="" line="" escaped="">