Sep 13, 2012

Magento: Get product attribute’s select option id/label/value

If you have a select dropdown for any product attribute, to get the value from label or vice versa is always needed in order to display or get value for further processing, etc. Every now and then you will require this values while working on product attributes. There aer many ways you can achieve it but the best, in terms of performance and simplicity is what I will tell you here. Get product attribute’s value from label, label from value easily in Magento.

Suppose, you have an product attribute called “color” in Magento. You have the label (e.g. Red), and you want to find it’s value. The below code will help you get the value for it.

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
    echo $color_id = $attr->getSource()->getOptionId("Red");

Now suppose, you have the value (let’s say 8, for Red) for your attribute, but want to get the label for it. Below code will help you to achive it.

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
    echo $color_label = $attr->getSource()->getOptionText("8");

Note that the only thing changed in both is, getOptionId() and getOptionText. getOptionId() will accept label and give you value, while getOptionText() will accept value and give you label.


  • I came across this situation before. This is very informative. Thanks!

  • This is very helpful for beginners like me… Thanks for sharing this code also post new codes in this blog

  • good article

  • Very useful, many thanks.

  • very useful

    you can also look at this method and such tools

  • nice!

  • Thank you! 🙂 this post is very helpful to me . Thank you once again 🙂

  • Thank you, you helped me a lot.

  • nice , helped me a lot

  • Thank you! It really helped me out of trouble!

  • Perhaps you can give me a clue about this problem relating to Product Color…

    I’m creating copies of attribute sets for various products because their marketing color names vary across all the products.

    I’ve done two ranges of products and already I have about 20 attribute sets for 20 configurable products for the sole purpose of listing the right variations of colors for the respective products and I’ve done about 20% of all products.

    I’m sure I’m going about this the wrong way.

    It would make more sense to have a table of products and a comma delineated list for each color which is generated in admin when I create a product. This would then mean I have one single configurable product attribute set.

    Also their would need to be another table for the filter so that I have a general color list where for each color I have a corresponding comma delineated list of equivalent colors
    for eg.
    White | “Arctic, Cream. Off White, Snow, Bone”
    When white is selected in the filter any corresponding colors from the table would generate a match and the respective products would be displayed.

    This should be a relatively common issue yet I’ve found nothing on the subject. What/Where should I be searching for – What am I missing

  • I want to get the values of 1 product which have multiple attributes. i want to get their attribute values as dropdown . i am not able to get value if i selected both attribute like name & one dropdown attribute value .

    $unit_type = $_product->getResource()

    what code i should use to achieve 3rd attribute value based on 1st two attribute options for a single product.

  • Awesome. It works!

  • Thanks You bro keep good

  • Thank you! 🙂 this post is very helpful to me . Thank you once again

Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors