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

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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen 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. Devin R. Olsen Roshan says:

    Thanks For Help …….

  10. Devin R. Olsen 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. Devin R. Olsen Bob says:

    Not really serious a website without Google+ +1…

Leave a Reply