How to override magento core blocks, controllers, models, helpers  ?

How to override magento core blocks, controllers, models, helpers ?

If you are a magento developer and some how you are in need of rewriting a magento core classes for any purposes, should you modify the files directly ? Of course the answer is “No” as it’s not recommended and also not necessary. Magento already provided us an excellent way for doing just that

Firstly we might need to create a new magento extension to locate our new class, for quickly create a new magento extension you can use magento module creater . On this post i will be overrring Magento customer classes

Below are more details on how to override/overwrite blocks, controllers, models, helpers

Overriding/extending Magento core blocks

Suppose that here we want to add some functions to core customer account block so we will extend the ogirinal classes with all of it’s functions

Firstly you need to add a rewrite tag for the customer account block to current extension’s config.xml file, open the file to add the code below

<config>
    <global>
        <blocks>
            <customer>
                <rewrite>
                    <account>Mageaddon_Customer_Block_Account</account>
                </rewrite>
            </customer>
        </blocks>
    </global>
</config>

As defined above you need to create a block file in app/code/local/Mageaddon/Customer/Block/Account.php with the code below

   class Mageaddon_Customer_Block_Account extends Mage_Customer_Block_Account
   {
    // your code goes here
    }

Same as above, if you want to extend a adminhtml block,for example Mage_Adminhtml_Block_Customer_Edit you would need to define a rewrite rule as below

<config>
    <global>
        <blocks>
           <adminhtml>
             <customer>
                <rewrite>
                    <edit>Mageaddon_Customer_Block_Adminhtml_Edit</edit>
                </rewrite>
             </customer>
           </adminhtml>
        </blocks>
    </global>
</config>

and then add a class to app/code/local/Mageaddon/Customer/Block/Adminhtml/Edit.php with the code

 class Mageaddon_Customer_Block_Account extends Mage_Adminhtml_Block_Customer_Edit
 {
   // your code goes here
 }

Overriding/extending Magento core models

Overriding core models would be the same as blocks, beside models classes we can also override Collection and Resource classes, suppose that we need to extends these classes

       Mage_Customer_Model_Customer
       Mage_Customer_Model_Resource_Customer
       Mage_Customer_Model_Resource_Customer_Collection
      

as usual,Firstly we need to define rewrite rules in config.xml file

 <config>
    <global>
        <models>
            <customer>
                <rewrite>
                    <!-- Model -->
                    <customer>Mageaddon_Customer_Model_Customer</customer>
                </rewrite>
            </customer>
            <customer_resource>
                <rewrite>
                  <!-- Resource -->
                  <customer>Mageaddon_Customer_Model_Resource_Customer</customer>
                  <!-- Collection -->
                  <customer_collection>
                    Mageaddon_Customer_Model_Resource_Customer_Collection
                  </customer_collection>
                </rewrite>
            </customer_resource>
        </models>
    </global>
</config>

And next we have to create new class files at

       app/code/local/Mageaddon/Customer/Model/Customer.php
       app/code/local/Mageaddon/Customer/Model/Resource/Customer.php
       app/code/local/Mageaddon/Customer/Model/Resource/Customer/Collection.php
       

Overriding/Extending Magento core helpers

With the same principle above magento helpers can easy be overriden too, firstly define rewrite rule in config.xml

    <config>
        <global>
          <helpers>
            <customer>
                <rewrite>
                    <data>Mageaddon_Customer_Helper_Data</data>
                </rewrite>
            </customer>
          </helpers>
      </global>
    </config>
   

And then create a helper file at app/code/local/Mageaddon/Customer/Helper/Data.php with the code

    class Mageaddon_Customer_Helper_Data extends Mage_Customer_Helper_Data
    {
      // your code goes here
    }
 

Overriding/Extending Magento core helpers

Overriding/Extending magento controlers are different than the previous examples because we can not apply the rewrite rule for controllers, so what would we do if we would like to rewrite controllers in Mage_Customer_Account ? let start with what need to be defined in config.xml

   <config>
     <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <mageaddon_customer before="Mage_Customer">Mageaddon_Customer</mageaddon_customer>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
  </config>
   

Here we simply add our module to customer front-end router, notice that before=”Mage_Customer” will load our controller first if available and fall back to Magento’s if not.You can see that no specific controler file is defined here we just defined the path that magento will look for to find controller files for the Customer router. If you want to override app/code/core/Mage/Customer/controllers/AccountController.php you have to create a file with the same name at app/code/local/Mageaddon/Customer/controllers/AccountController.php with the below code

    require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');
    class Mageaddon_Customer_AccountController extends Mage_Customer_AccountController
    {
      // your code goes here
    }
  

Do the same as above to override adminhtml’s controlers, start from config.xml

    <config>
     <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <mageaddon_customer before="Mage_Adminhtml">Mageaddon_Customer_Adminhtml</mageaddon_customer>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
  </config>
 

Then create a controller file at app/code/local/Mageaddon/Customer/controllers/Adminhtml/CustomerController.php with the code

 require_once(Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'CustomerController.php');
 class Mageaddon_Customer_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController
 {
  // your code goes here
 }
 

We have just go through some Magento rewrite code, let start writing your own rewrite class now

About Dat huang

Dat is a founder and Developer at Mageaddon.He is a magento lover, he enjoys listening music, playing table tennis, swimming whenever he is Free

6 comments

  1. This web site really has all of the information I
    wanted concerning this subject and didn’t know who to ask.

  2. Nice Blog, thanks for sharing this kind of information.

  3. Greetings! This is my 1st comment here so I just
    wanted to give a quick shout out and say I truly enjoy reading your posts.
    Can you recommend any other blogs/websites/forums that go over the same
    topics? Thank you so much!

  4. Youг mode oof tеllіng еνerything in this piece of writіng is really good, every one be аble to easily understand it, Thanκs a lot.

Leave a Reply

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

*