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
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
Thanks! Just what I was looking for.