Overriding Magento blocks

February 16, 2016
Posted by admin 9:48 AM

 In magento if we have to enhance some block classes of core files then according to 

 magento standard format we have to override that class in our own Local Module.

For that we need to create our own module and to make the chnages in local module

config.xml file and some changes in our own module Block classes.

 To override the block classes we have to add <rewrite></rewrite> tag under module name.

 

Let us take an example that we need to override Mage_Checkout_Block_Onepage_Success class.

then below is the code we need to write in our config file inder global node.

 

Our own module name is Hello module and in we need to write the code at below path

 app/code/core/local/Hello/Checkout/etc/config.xml

 <config>

     <global>

          <blocks>

                <checkout>

                      <rewrite>

                               <onepage_success>Hello_Checkout_Block_Success</onepage_success>

                   </rewrite>

             </checkout>

        </blocks>

 </global> 

</config>

 

and as per the core class path we have to create a file at below path

 app/code/core/local/Hello/Checkout/Block/Onepage/Success.php

 and in this file the code will be

 class Hello_Checkout_Block_Onepage_Success extends Mage_Checkout_Block_Onepage_Success

{

  // module code according to requirment

}

 Now magento will read Hello_Checkout_Block_Onepage_Success code first before Mage_Checkout_Block_Onepage_Success code according to magento code pool standard.