How to get attribute option label in Magento with different store view

October 29, 2010 11:37

In Magento customization, sometimes you may want to get the value of attribute’s option. In below example I will show you how to do it.

Assuming that there is an attribute named “dropdown_field” and contains 4 options label: “Option 1″, “Option 2″, “Option 3″, “Option 4″. The codes below get the option label which is set in product:

// get product Model
$_product = Mage::getModel('catalog/product');

// get the attribute object
$dropdownAttributeObj = $_product->getResource()->getAttribute("dropdown_field");

// if input type for this attribute is dropdown or multi-select
if ($dropdownAttributeObj->usesSource()) {
    // get the option label
    $dropdown_option_label = $dropdownAttributeObj->getSource()->getOptionText($_product->getData('dropdown_field'));

How about multiple websites / store / store view? No worry, it’s automatically get the correct label for you; or you can use $this->__() translation method in your template files.

Happy Magento’ing ~


Comments are closed.