How to rewrite blocks in magento

This is our Rewrite series and in this whole series we will be learning “How to Override Blocks,Modules,Helpers and Controllers” classes of frontend and admin both in Magento.

We hardly find a project that uses framework as it is. Every time there is a need to change something in the core, to add something new or to override/rewrite something already written to meet our custom requirements. We can modified core files directly but it is very bad programming habit to modify core files directly and it is also not recommended at all due to  many reasons. For example, if we modify core files directly then after some time if there is a need to update framework/magento then our custom changes in core files will be overridden.

So, every framework support extendability and scalability in the core itself. So does magento, Magento brought in an excellent way how to overwirte/override core files to alter the flow and behavior of the core features of Magento.

Overriding Magento Frontend Blocks

Let’s suppose if there is a need to change something in Catalog Core Blocks, like if we want to change something in Core Product List Block “Mage_Catalog_Block_Product_List” class. So here we would like to add our own File which will extend the core file with all of its methods and our own custom functions.

So here we will be creating our custom module which will override the core  functionality of Catalog Product List Block. Following files needed for our custom modules.

  • app/etc/modules/Magehelpers_Catalog.xml: It’s a file used to enable our custom module.
  • app/code/local/Magehelpers/Catalog/etc/config.xml: It’s a module configuration file in which we’ll set up block overriding using certain tags as per the Magento conventions.
  • app/code/local/Magehelpers/Catalog/Block/Product/List.php: It’s our new block file which will replace a core block file

Let’s create files and folders for our Custom Module:

Step 1: Activating your module: Go to the app/etc/modules directory and create the Magehelpers_Catalog.xml file and paste the below code in the file:

 

Step 2: Next we need to create module configuration file,this file will tell magento everything about the structure of our module. Create file “app/code/local/Magehelpers/Catalog/etc/config.xml” and Put the following code in that file:

Step 3: Finally create Block class Magehelpers_Catalog_Block_Product_List,

Create file “app/code/local/Magehelpers/Catalog/Block/Product/List.php” and Put the following code in that file:

,

This is First article of our rewrite series, in this whole series we will be learning overriding frontend and admin Blocks,Modules,Helpers and Controllers.Please subscribe to my blog to get in touch with latest posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen + 15 =