Monday, March 30, 2015

Generate static block dropdown in System Configuration in Magento

>    First you need to create the system configuration menu. You'll find many tutorial to create system configuration menu. So, in your system.xml you need add a Source Model like "module/system_config_source_cms_block". Now create a directory in your module: YoureModule/Model/System/Config/Source/Cms/Block.php

Now your Block.php looks like below :

class CompanyName_YourModule_Model_System_Config_Source_Cms_Block

    protected $_options;

    public function toOptionArray()
        if (!$this->_options) {

$collection = Mage::getModel('cms/block')->getCollection();
$staticBlock = array(''=>'-- Select Static Block --');
foreach($collection as $block){
$staticBlock[$block->getIdentifier()] = $block->getTitle();

            $this->_options = $staticBlock;
        return $this->_options;


Refresh cache to see changes.

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

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;

$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,


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