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());



Friday, March 14, 2014

get catalog rule applied price of product in Magento


 >> We use to get product price like :
                       $_product->getPrice();
                       $_product->getFinalPrice();
     
      Here you can get the catalog rule applied price like:
            $store_id = Mage::app()->getStore()->getId();
            $discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice(
                                                      Mage::app()->getLocale()->storeTimeStamp($store_id),
                                                      Mage::app()->getStore($store_id)->getWebsiteId(),
                                                      Mage::getSingleton('customer/session')->getCustomerGroupId(),
                                                                      $_product->getId());
               
           
            if ($discountedPrice===false) { // if no rule applied for the product
                $discountedPrice = $_product->getFinalPrice();
            }else{
                $discountedPrice = number_format($discountedPrice,2);
            }