In this article, we are going to discuss about How to override/rewrite the model class in Magento. In most of the cases we need to extend the Magento core functionality. For that we have to copy the core model class file to the local directory from core. Then, we can override/rewrite the core model class.
Here we are going to discuss about How to override the sales order model (Mage_Sales_Model_Order class).
Step 1 : Create a config.xml file for the new module
Create a new config.xml file in "/app/code/local/MyCompany/NewModule/etc/" folder and add the below codes in the config.xml file.
<global>
<models>
<sales>
<rewrite>
<order>MyCompany_NewModule_Model_Order</order>
</rewrite>
</sales>
</models>
</global>
By using the above code, we have overloaded the Mage_Sales_Model_Order class with the MyCompany_NewModule_Model_Order class.
Step 2 : Create the new module
After creating a config.xml file, we need to create a new extended order class "order.php" file in "/app/code/local/MyCompany/NewModule/Model/" folder and add the below code in the order.php file.
<?php
class MyCompany_NewModule_Model_Order extends Mage_Sales_Model_Order
{
public function newFunctions()
{
//new functions/methods content
}
public function existingFunctions()
{
//new functions/methods content with the extended functionalities
}
}
We have created a new model class which extends the core model class. That means the new class will have all the properties and functions the core class has. All the functions we put in our new class will be used instead of the core classes functions, and we can also add completely new functions to it.
Step 3 : create the new modules initialization xml
Create a "MyCompany_NewModule.xml" file in the folder "/app/etc/" and add the below code in the file.
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_NewModule>
<active>true</active>
<codePool>local</codePool>
</MyCompany_NewModule>
</modules>
</config>
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.
Here we are going to discuss about How to override the sales order model (Mage_Sales_Model_Order class).
Step 1 : Create a config.xml file for the new module
Create a new config.xml file in "/app/code/local/MyCompany/NewModule/etc/" folder and add the below codes in the config.xml file.
<global>
<models>
<sales>
<rewrite>
<order>MyCompany_NewModule_Model_Order</order>
</rewrite>
</sales>
</models>
</global>
By using the above code, we have overloaded the Mage_Sales_Model_Order class with the MyCompany_NewModule_Model_Order class.
Step 2 : Create the new module
After creating a config.xml file, we need to create a new extended order class "order.php" file in "/app/code/local/MyCompany/NewModule/Model/" folder and add the below code in the order.php file.
<?php
class MyCompany_NewModule_Model_Order extends Mage_Sales_Model_Order
{
public function newFunctions()
{
//new functions/methods content
}
public function existingFunctions()
{
//new functions/methods content with the extended functionalities
}
}
We have created a new model class which extends the core model class. That means the new class will have all the properties and functions the core class has. All the functions we put in our new class will be used instead of the core classes functions, and we can also add completely new functions to it.
Step 3 : create the new modules initialization xml
Create a "MyCompany_NewModule.xml" file in the folder "/app/etc/" and add the below code in the file.
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_NewModule>
<active>true</active>
<codePool>local</codePool>
</MyCompany_NewModule>
</modules>
</config>
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.
Nice article.
ReplyDeletePlease keep in mind that rewriting classes in Magento is not the best way of extending the CORE since you can how conflicts with other modules, Magento Event Observers are the best non intrusive way to interact with the CORE, I recognize though that sometimes you have to create a rewrite since there is no event available for what you need.
Cheers!