Customize Magento functionality: Override core code

The very first thing any beginner in magento development learns is this: DO NOT change core code! It is absolutely necessary that we don’t make our customizations in the base code of magento. The main reason for that is core files get overwritten when we upgrade to newer version of magento which in turn removes any changes whatsoever we did. Also it is best practice to just let the core alone and do changes elsewhere. That brings us to the topic of discussion: overriding magento core code.

So now we know WHY we need to override classes but HOW does it actually happen? For those of you who don’t know the basic structure of files/folders in magento, here is a quick look. There are two different themes applied to your magento site at any given point of time. The one you specifically select to be applied and the one that gets implemented by default in case your current theme fails due to any reason. Just like the theme (read design), the code is also implemented twofold. Magento looks for code to run in your current theme. If there is some file missing or some code missing it will go to the base theme and get the code/ design from there. So basically when you want to customize anything just write code for it in your current theme. So magento will execute that first.

This sounds nice and dandy.. Write our code in the current theme and changes will take effect. But when you create a module; things kind of change a little bit. You no longer work on the same file structure as your magento base. The module you created is not part of the magento that we get out of the box. Now you want to enhance some functionality of magento using your new module instead of making changes directly in your installation. The concept is still the same with a little bit of twist. You put the layout and templates of your module in the theme you want it to display in. So magento will run it from there.

But when you want to use some already existing functionality in magento a little differently you need to override the underlying code and write your own.I am going to describe how the whole process works from the scratch. There are only three basic steps you need to follow.

1) Copy core files in your module: Copy the core files that you are going to use for your own needs in your module.

2) Rename the class: Rename the class in your module’s file and extend it to the original class in that file.

3) Rewrite the class in the config file: By doing this you let magento know that you have created a new class and that it needs to be executed instead of the original class defined in the core file.

Let us check all three steps in detail now. Suppose you have created a module named Mymodule. It should be in the local folder of your magento root. The folder structure of your module will be like this.


Now we want to use a class that is defined in the checkout module of core magento. We need to add some additional functionality when the user adds items to the cart. For that magento uses the file stored here.


We will override this class and put our additional function in it or maybe some updates in the existing functions. First copy this file to your module. I have a simple strategy when it comes to determining the place to copy core files in our modules. User the same structure as magento core is using. Helps us keep track of files and especially useful when some file is not getting called we can compare it to the core structure and IMHO it is just a clean approach. So copy this file in local so that file structure is like this :


You can place the file anywhere in your namespace but it is advisable for the reasons above to put it where we can find it easily.The file is copied.

Now we come to the second step of our process. Rename the class. The original file has a class name that looks something like this.

class Mage_Checkout_Model_Cart extends Varien_Object{

//core code


What we need to do is rename the class to match the location that we just changed and extend it to this original class. So that it looks like this:

class Mynamespace_Checkout_Model_Cart extends Mage_Checkout_Mode_Cart{

//your code


You can either create new functions or add more code to make the already written functions in this class customized to your needs. Which takes us to our last and important step. Rewrite the class in config file. After adding your new class you now need to let magento know that you have created it and that it should run it first now in place of the original class. For that open the config file of your module, which is located at :


Rewrite the class name in config file of the module under <models> tag as we are rewriting the class of a model.




And voila!! It’s done. Now cross your fingers and clear your cache! Test your module.

Keep coding🙂

Posted in Magento. 1 Comment »

One Response to “Customize Magento functionality: Override core code”

  1. nodoubt Says:

    Replace : Mynamespace_Checkout_Model_Cart
    By : Mynamespace_Checkout_Model_Cart

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: