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




Tuesday, December 3, 2013

How to create custom category attribute in Magento


>> If you need to create a category attribute there's no option in magento admin. You need to create it through code or sql. I'm showing the two:

1) Here you can use this code with a new module or create a upgrade sql  for an existing module and write below code by changing your attribute code/Label name.

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'custom_attribute', array(
    'type' => 'int',        // "varchar" for input type text box
    'group'     => 'General Information',
    'backend' => '',
    'frontend' => '',
    'label' => 'Custom Attribute',
    'input' => 'select',    // "text" for input type text box
    'class' => '',
    'source' => 'eav/entity_attribute_source_boolean',            // Leave blank for input type text box
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'default' => '0',        //Leave blank for input type text box
    'searchable' => false,
    'filterable' => false,
    'comparable' => false,
    'visible_on_front' => false,
    'unique' => false,
));

$installer->endSetup();

                                                                           OR
2)  Or you can run direct sql like this way:

        INSERT INTO `eav_attribute` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`) VALUES ('', 3, 'mp3', NULL, '', 'varchar', '', '', 'text', 'Mp3 URL', '', '', 1, 0, '', 0, '');

**** Suppose last inserted ID is 134. This ID will place in the following two table.

        INSERT INTO `eav_entity_attribute` (`entity_attribute_id`, `entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES ('', 3, 3, 3, 134, 10);

   Note:- Please check in your database for your attribute type_id , set_id and group_id.

        INSERT INTO `catalog_eav_attribute` (`attribute_id`, `frontend_input_renderer`, `is_global`, `is_visible`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_html_allowed_on_front`, `is_used_for_price_rules`, `is_filterable_in_search`, `used_in_product_listing`, `used_for_sort_by`, `is_configurable`, `apply_to`, `is_visible_in_advanced_search`, `position`, `is_wysiwyg_enabled`, `is_used_for_promo_rules`) VALUES (134, '', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, '', 0, 0, 0, 0);


Note* : Will suggest to follow the first step and follow the second to know the table reference.



Friday, November 29, 2013

How to remove State/Region from Estimate Shipping in Magento


If you want to remove the State/Region dropdown from Estimate shipping block. Find the code
new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);  and comment out in the app/design/frontend/enterprise/default/template/checkout/cart/shipping.phtml directory



Tuesday, August 13, 2013

Programmatically change product attribute value in a store Magento


>> We have already known that magento has the save() method to save the data through model. We can save a product data programmatically with the product object. Here's a example:

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setData('name','value');   OR   $_product->setName('value');
            $_product->save();
            // With this we can change the product name. But the save() method will save globally.

Now suppose you need to change a product visibility for a particular store. Then using this code you can change a particular attribute for a store without saving entire product attributes. Here's it :

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setStoreId(1);
            $_product->setVisibility(1);
            $_product->getResource()->saveAttribute($_product, 'visibility');