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

Friday, March 14, 2014

get catalog rule applied price of product in Magento

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

Thursday, February 6, 2014

Convert special character to normal character in PHP

    > You need to convert special character to normal character. Below are the list of some special character:
    $special_chars = array(
         Now try the below code to convert these to normal character :
        $text = "éèçâñüÄ";
        $htmlchar = htmlentities($text, ENT_COMPAT, 'UTF-8');  //éèçâñüÄ
        $text = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($text, ENT_COMPAT, 'UTF-8'));  //eecanuA


Tuesday, January 21, 2014

Uses of $this->getChildHtml() in Magento

        >> $this->getChildHtml() is a known features to include a child block in a page. I'm simplifying this with an example:
      In your XML : 
       <block type="core/template" name="parent_block" template="example/test.phtml">
             <block type="core/template" name="child_block" template="example/test_child.phtml"/>

      Now in your test.phtml you can include the child block like : 
             <?php echo $this->getChildHtml('child_block'); ?>

       >> You can also pass a variable to this block. I've shown this in my previous post. Now if you want to pass dynamic value to the child block, here's it:

                $this->getChild('child_block')->setData('product_id', $productId);
                echo $this->getChildHtml('child_block', false);

         Here $productId is your dynamic value and you need to pass "false" (Block Caching No) as  additional parameter in childHtml(). By default it takes "true" (Block Caching Yes).
        You can get the product id in test_child.phtml with $this->getProductId()