How to Obtain Custom Attribute Values in Magento

Today let’s talk about Magento and custom attributes. With Magento you can create as many custom attributes for your products as you like, and there are different attribute types such as:

  • Text Field
  • Text Area
  • Date
  • Yes/No
  • Multiple Select
  • Dropdown
  • Price
  • Gallery
  • Media Image
  • Fixed Product Tax

Because there are different types of attributes there are also different ways of obtaining an attributes value. If we were to create a new custom attribute called “theme_color” and the attribute was of a “Text Field” type, we could do something like the following at a product level (meaning a product is loaded) to obtain its value.

<?php
echo $_product->getThemeColor();
?>

$_product is the product of question we have loaded up and ready to work with, and we use “getThemeColor();” because our attribute was named “theme_color”. It’s important to realize that when calling out the value of an attribute with this method, we start with “get” and then capitalize the first letter of each word that is divided by a “_”.

Now this method would give us the value of a simple “Text Field” attribute but what if the attribute was a “Dropdown or a Multiple Select” type? Well if you try to use this “getYourAttributesName()” method on a none “Text Field” or “Textarea” attribute type you will most likely get nothing.

So how do we obtain the value of a “Dropdown or a Multiple Select” attribute type you ask? Well some would argue that you would do so via this method:

<?php
echo $_product->getAttributeText('theme_color');
?>

However, what this would do is return an array of the “Dropdown or Multiple Select” options instead of the value of the selected option you would have set. There is another way to obtain these types of attributes selected value and it looks like this:

<?php
$attributes = $_product->getAttributes();
$themeColor = $attributes['theme_color']->getFrontend()->getValue($_product);
echo $themeColor;
?>

With this method we are simply creating a new variable called “$attributes” and pointing to our “$_product” to then using the “getAttributes()” function to gather all the attributes of this product.

Once we have all the product’s attributes inside of the “$attributes” variable, we make another new variable called “$themeColor”. “$themeColor” then assigns its self to our “$attributes” array that we in turn call out our custom attribute [‘theme_color’].

Once we have pointed to the custom attribute in the “$attributes” array, we simply run through the front end via the function “getFrontend()” and then pull the value down via “getValue($_product)”.

If we then echo our “$themeColor” you should get the value of your custom Drop Down or Multiple Select attribute to do what you will with it.

So to recap:

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

Would get the value of an attribute wholes type is of Text Field or Text Area’s

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

That will not give a value of a Text Field or Text Area attribute type but rather gather an array of all options in a Drop Down or Multiple Select attribute type.

<?php
$attributes = $_product->getAttributes();
$themeColor = $attributes['theme_color']->getFrontend()->getValue($_product);
?>

That will give you the value of any type of attribute, even the value from “Drop Down or Multiple Select” attribute types.

I hope this clears some things up and helps some developers out there .

Thanks!

Devin R. Olsen

19 Responses to “How to Obtain Custom Attribute Values in Magento”

  1. carin says:

    Thanks a lot, you helped me to get my head around this issue.

  2. Mitch Thompson says:

    Don’t forget, for those methods to work, the attribute’s setting for “Used in Product Listing” must be set to yes. Otherwise you’ll just get “No” or Null or some other confusing value.

  3. Matthias says:

    Sir, thank you!

  4. Nguyen Danh Tung says:

    Hi!
    I create a attributes name is allcolors in configurable product used to load all color of simple product
    allcolors atrribute is type multiselect dropdown!
    so! how to update or add attribute following product
    Please help to me! thanks you very much!

  5. Vishal says:

    Hi.

    It is great. But i have a question. If i take image attriute.so how to get image on front end.

    Thanx
    Vishal Sanwar

  6. Paul says:

    Hi,
    Thanks for the excellent post.
    I am wanting to put a quantity of ‘Each’ ‘Box of 4′ after the price. I’ve created a custom attribute as a drop-down and called it as suggested. This is working fine on all product pages but not on the category pages (grid & list). On these the output just renders as ‘No’. What have I missed?

  7. Hans says:

    Hi Devin,

    thanks for this nice tutorial. It works great! I have a special case and found no post on the internet so far. I have a multi select color attribute and want to show images instead of the color value.
    e.g. img src=”../../admin_attribute_value.jpg”. The attribute value has to be the “admin” attribute, because I have multiple translations for the standard attribute and blue.jpg would not work for bleu or blau in different shop views.
    Do you have a solution for showing images based on multi select attributes?

    I really appreciate your help!

    Thanks

  8. waqar ahmed says:

    This is great post and saved lot of time to me. :) Thanks

  9. Adrien says:

    Hello,

    For customizing a specific configurable product, I created a module.
    I try as possible to retrieve the values ​​of the attributes of my product. Not the value selected by the customer, but all possible values ​​of the attribute.

    Can anyone help me?

    Sorry for my English. grin

    Thanks ++

  10. Adrien says:

    Hello!

    I’d like to retrieve all possible values ​​of an attribute for a certain product.
    I’d like to show my attribute values ​​of another so that the SelectList.
    How can I do?

    Thanks

  11. Parmelia says:

    Thanks for sarhnig. Always good to find a real expert.

  12. Xadrian says:

    Hey, that post leaves me feeling fooilsh. Kudos to you!

  13. Rich says:

    GREAT Tutorial!

    I have been searching all over the web on how to get a Date attribute to show. This worked the first time. I wish all Magento tips/tutorials were written this well.

  14. @Cliff, you should be able to use something like if($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE). However I also read that $product->getTypeId() returns an index number of all the different product types. Its worth a try and some investigating.

  15. Cliff says:

    This is working for me to be able to display attributes for simple products, but it is causing an issue when I browse a configurable product. What code would I use to test If the product is simple type and only pull in the attribute data for simple products.

    I am new to Magento but have learned so much. This was a good post for me.

    Thanks so much!

  16. @paul liu – Please again note that this tutorial assumes you have the product or even category in question loaded up before one can successfully use this method.

    Because products are most popular, lets assume you are out in your products template/catalog/product/view.phtml file. One must first load up the product out of the $this variable before one can obtain its attribute values, like so.

    $product = Mage::getModel(‘catalog/product’)->load($this->getProduct()->getId());
    echo $product->getCustomAttribute();

    Please learn how to properly use Magento before bashing a perfectly good solution or article. Better yet please ask for help or assistance first before assuming its the articles fault.

  17. paul liu says:

    not work at all

  18. evony hot says:

    Such a well written post.. Thnkx for sharing this post!

  19. I always enjoy feedback from my readers!

Leave a Reply