3

I have created a custom attribute (color_materials) in Magento admin, which input type is set to dropdown. I'm trying to get this attribute value programatically, and I have the following code.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->getSku($_item));
$productOption = $product->getData('color_materials');

The output i got is a number, which is stored in catalog_product_entity_int. But I want to get it's associated text, not a number. How can I achieve this?

2

use getAttributeText

 $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->getSku($_item));
 $productOption = $product->getAttributeText('color_materials');
  • Thanks. It worked. Can you also tell me where to get a full list of Magento functions? it was simply because I didn't know there's a function called getAttributeText(). – Magento Learner Mar 22 '17 at 7:36
  • @Zhenyu the function is located app/code/core/Mage/Catalog/Model/Product.php on line 1381 function getAttributeText – Qaisar Satti Mar 22 '17 at 7:39
1

Since your attribute type is dropdown $product->getData('color_materials'); will return you the value.

Since you need the label you can use

$product->getAttributeText("color_materials");

Other ways

$id = $this->getProduct()->getId();
$_resource = Mage::getSingleton('catalog/product')->getResource();
$optionValue = $_resource->getAttributeRawValue($id,  'color_materials', Mage::app()->getStore());
echo $optionValue;
1
$product->getData('color_materials'); 

OR

$product->getColorMaterials();

Return the attribute option Id , in case of dropdown or multiselect.

For fetching attribute option text you have to write below code

$product->getAttributeText('attribute_code'); 

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.