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?

asked Mar 21, 2017 at 13:03

3 Answers 3

3

use getAttributeText

 $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$this->getSku($_item));
 $productOption = $product->getAttributeText('color_materials');
answered Mar 21, 2017 at 13:06
2
  • 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(). Commented Mar 22, 2017 at 7:36
  • @Zhenyu the function is located app/code/core/Mage/Catalog/Model/Product.php on line 1381 function getAttributeText Commented Mar 22, 2017 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;
answered Mar 21, 2017 at 13:18
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'); 
answered Mar 22, 2017 at 7:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.