Wednesday, March 19, 2014

Magento :: $_product->getAttributes() not working in listing page

     
>>  With the following code every-time you'll get same attributes as it first load the attributes of product and store in a variable. Then from the second time it'll check the variable for the attributes.

        $attributes = $_product->getAttributes();
        foreach ($attributes as $attribute) {
            if ($attribute->getIsVisibleOnFront()) {
                $value = $attribute->getFrontend()->getValue($_product);
                $attributeLabel = $attribute->getFrontendLabel();
                $attributeCode = $attribute->getAttributeCode();
            }
        }
       
        In the above code you'll get the product attribute details and value in listing page if the attribute_set_id for all products in listing page is same. But if the attribute_set_id of all products are different, you need to change your code:
                $attributes = $_product->getAttributes();  
                //Replace this with the following
                $attributes = Mage::getModel('catalog/product')->getResource()
                                      ->loadAllAttributes()
                                      ->getSortedAttributes($_product->getAttributeSetId());