1

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 ?

1
  • SO this was right: echo $data = $_product->getAssembly(); echo $label = $_product->getAttributeText('Assembly'); the only problem was capital "A" :) Thank you – Rob D. A. Feb 20 '17 at 14:04
3

$_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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.