Jun 15, 2013
kalpesh

Magento get attribute options

Get attribute options list i.e. label and value, if the attribute type is dropdown.

This post will show you how to get all the options of attribute with dropdown type in Magento. If your attribute has options, below code will give all the attribute options labels and values in an array format.

$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'shirt_size'); //change shirt_size to your attribute code
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $option) {
        print_r($option);
    }
}

In the above code, first line will initialize the attribute. Then we are checking if the attribute is using source model or not, if using then get all the options of that attribute.

It will output this for attribute shirt_size:

Array
(
    [value] => 100
    [label] => Small
)
Array
(
    [value] => 99
    [label] => Medium
)
Array
(
    [value] => 98
    [label] => Large
)

If you are here to get attribute option’s label from value OR get attribute option’s value from label, just check out this post: http://ka.lpe.sh/2012/09/13/magento-get-product-attribute-select-option-idlabelvalue/

1 Comment

  • Thanks! Just what I was looking for.

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!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions