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




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"/>
       </block>

      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:

           <?php
                $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()