How to Get Selected Option of Product Attribute in Magento 1.9

attributeslabelsmagento-1.9option

Struggling to get value of the selected option for product.

Example:Product require assembly so created attribute dropdown
Below few methods I've tried

echo $data = $_product->getAssembly();
echo $label = $_product->getAttributeText('Assembly');

$_attribute = $_product->getResource()->getAttribute('assembly');
$attribute_label = $_attribute->getStoreLabel(Mage::app()->getStore()->getName());

echo 'Label: '.$attribute_label;//empty
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','assembly');

$attributeModel->getData('label');
echo 'Model: '.$attributeModel;// empty
$atributeCode = 'assembly';

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$atributeCode);
$options = $attribute->getSource()->getAllOptions();

var_dump is displaying only an array of all options.

How can I get it ?

Best Answer

$_product->getAssembly() should return you the id of the option selected for the current product.

$_product->getAttributeText('assembly') (notice the lowercase attribute name) should return you the label of the selected assembly option for the current product.

But there is a catch.
If you are trying these in the product list page or somewhere where the $_product is one iteration from a product collection you have to mark the assembly attribute as "Used in product listing" and rebuild your product flat indexes in order to work.

Related Topic