Magento: How to override/rewrite model class in Magento

Magento: How to override/rewrite model class in Magento

In many cases we need to extend a Magento core functionality.
What should be the best way to do this? We could just copy the core model class file to the local directory from core. This could be the easiest way, but far from the best.
The cleaner way would be to override/rewrite a core module class.
How to do that? Well, bellow we will present an example.

Override the sales order model (Mage_Sales_Model_Order class):

1. New modules config.xml
In the custom modules/extensions config.xml file we should have something like this in the bellow directory:


With this we have overloaded the Mage_Sales_Model_Order class with the MyCompany_NewModule_Model_Order class.

2. Create the new module:
Then we have to create the new extended order module class in:

3. Finally create the new modules initialization xml:

<?xml version="1.0"?>

By placing the above file in that directory, Magento will know that it has to look for a config.xml in /app/code/local/MyCompany/NewModule/etc and load our module.

That’s all. Try it yourself.

Pin It on Pinterest

Share This