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.

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:

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:

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:

getThemeColor() ?>

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

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.

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

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:
TwitterFacebookGoogle Plus

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

  1. Tony says:

    I have an attribute that is multiselect with the values 5,6,7,8,9,10
    When I echo it out, it says 5,6,7,8,9,10

    How would I make it just display the first and last values?

    For Example:

    Sizes: 5-10

  2. chandra sekar says:

    I am new to magento. I am using market place extension for that I want your help to create invoice. In that i want to print seller address, tin and vat no. Of product’s seller info. I store those information in marketpalce_sellerprofile table. Kindly help me get the value from table and print the same in invoice.

  3. Mike says:

    Hi Devin,

    Great tutorial, thanks!! Maybe you could help me with a little variation on what you have shared. I am trying to show 4 attribute values along with the attribute names and style it for presentation. Based on your first example I can grab the 4 attributes and present them
    getLength();
    echo $_product->getWidth ();
    echo $_product->getThickness ();
    echo $_product->getShaper ();
    ?>

    but they are presented in a horizontal line with no styling. I would like them to be presented vertically and would also like to show the attribute name. So basically it would be a sub-set of what is being displayed in the ‘additional info’ tab. Any guidance would be greatly appreciated. Thanks!

  4. silvian says:

    Hi
    I want to call some custom text filed attributes inside the product description.
    my attributes are called key1 , key 2 up to key15.
    i want to use them as key features .
    I tried to put like this inside the product description :
    getThemeColor();
    ?>

    But , it is not working , I only see this script on the frontend.

    The reason I need that is because I upload my products via csv and i use these key features in 3 different places : website , ebay , amazon. I only enter them once and they shall come in all those places . I also want to use them inside the product description page but not able to call them. Please help

  5. Dennis says:

    I am struggeling for about 10 hours now.

    What I am trying to accomplish:

    The simple php code to display the selected LABELS and VALUES of a multiselect product attribute in list.phtml

    I want to use the LABEL as icon image title and the VALUE as the actual image source.

    I have search Google and tried many many ways. But nothin is exactly what I need.

    Can you please please help me out?

  6. Rohan says:

    I have a custom price attribute name rec_price. I can’t seem to get it to display on the product listing page!?

    Any ideas?

  7. michael says:

    thanks for the post, I was actually able to follow it and im not really a programmer.
    however, im having an issue and im sure its very basic…
    can you help me please?

    I get this error when trying to run code below:
    PHP Fatal error: Call to a member function getFrontend() on a non-object in /var/www/scripts/query-NoFilter.php on line 65

    $products_model = Mage::getModel(‘catalog/product’);

    $row = array();

    $header = array(‘EntityID’,’SKU’, ‘Name’, ‘Short Description’, ‘Manufacturer’, ‘manufacturerData’, ‘Price’, ‘MSRP’,’manufacturer_sku’, ‘url_key’,’series’,’size’,’size2′);

    $row[] = $header;

    foreach ($products as $pid)
    {
    $prod = $products_model->load($pid->getId());

    $data = array();

    $data[‘entity_id’] = $prod->getId();
    $data[‘sku’] = $prod->getSku();
    $data[‘name’] = $prod->getName();
    $data[‘short_description’] = $prod->getShortDescription();
    $data[‘manufacturer’] = $prod->getAttributeText(‘manufacturer’);
    $data[‘manufacturerData’] = $prod->getData(‘manufacturer’);
    //$data[‘description’] = $prod->getDescription();
    $data[‘price’] = $prod->getPrice();
    $data[‘msrp’] = $prod->getMsrp();
    $data[‘manufacturer_sku’] = $prod->getData(‘manufacturer_sku’);
    $data[‘url_key’] = $prod->getData(‘url_key’);
    $data[‘series’] = $prod->getData(‘series’);

    $attributes = $prod->getAttributes();
    $size = $attributes[‘size’]->getFrontend()->getValue($prod);

    $data[‘size’] = $prod->getAttributeText(‘size’);
    $data[‘size2′] = $size;

    $row[] = $data;
    }

    I was assuming its maybe that in the foreach loop $prod is maybe just an id and not an entire object?
    my whole goal here is to create a csv file I can use, so how would I adapt your code to pull in this foreach loop please?

    thanks very much!

  8. […] For a more detailed example of displaying a drop down/multi-select check out Devin R Olsen’s post. […]

  9. Roshan says:

    Thanks For Help …….

  10. Sherry says:

    Nice! This was so clear and concise. Good Magento tutorials seem extremely hard to come by. Do you have a YouTube channel as well?

  11. Bob says:

    Not really serious a website without Google+ +1…

  12. […] http://www.devinrolsen.com/magento-custom-attributes-value/ Share this:TwitterFacebookLike this:LikeBe the first to like this. Categories: Magento | Leave a comment […]

  13. Will Aquino says:

    Great post, thanks a lot!

    I need to return an array of a Multiple Select attribute without the spaces between the values (eg: “red,blue,green” instead of “red, blue, green”). Is it possible?

  14. nXqd says:

    thanks man, good stuff :)

  15. Muhammad says:

    WoW. This worked like a charm. Exactly what I was looking for. Very well explained. 5 Stars

  16. mlclm says:

    Nice post, however I’ve been looking to show a FIXED TAX PRODUCT attribute, but none of these code snippets work to show it. I can’t seem to find any info on how to display FTP attribute on Magento. I’m on Magento 1.6 by the way. Do you have a clue how I could do that ? Thanks.

  17. Remya says:

    Hi

    i would like to know whether there is any method to obtain Simple attribute value (height,color,manufacturer) of an item while doing Shipping Quote Calculation.
    Please help me to come out of this issue

  18. carin says:

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

  19. 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.

  20. Matthias says:

    Sir, thank you!

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

  22. 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

  23. 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?

  24. 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

  25. waqar ahmed says:

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

  26. 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 ++

  27. 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

  28. Parmelia says:

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

  29. Xadrian says:

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

  30. 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.

  31. 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!

    • @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.

  32. @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.

  33. evony hot says:

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

  34. I always enjoy feedback from my readers!

Leave a Reply