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?
3 Answers 3
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– Magento Learner2017年03月22日 07:36:44 +00:00Commented Mar 22, 2017 at 7:36
-
@Zhenyu the function is located
app/code/core/Mage/Catalog/Model/Product.phpon line 1381 function getAttributeTextQaisar Satti– Qaisar Satti2017年03月22日 07:39:37 +00:00Commented Mar 22, 2017 at 7:39
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;
$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');