Magento 1.9 – Fixing `getAttributeText(‘attribute_code’)` Returning `false`

attributesmagento-1.9product

when I am using this code $product->getAttributeText('attribute_code');

It always return false in one of my script.

and if I do below way then it return right value why ??

$attr_option_val = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code');
$product->setData('attribute_code',$attr_option_val);
echo $product->getAttributeText('attribute_code');

Best Answer

Based on the comments...
If you are getting the product from a collection of simple products associated to the configurable product you need to add this in the config.xml of your module in order for your attribute to be picked up

<frontend>
    <product>
        <configurable>
            <child>
                <attributes>
                    <attribute_code />
                </attributes>
            </child>
        </configurable>
    </product>
<frontend>

and clear the cache.

Related Topic