Overriding Magento Models

February 16, 2016
Posted by admin 10:51 AM

In magento if we have to enhance some model classes of core files then according to 

 magento standard format we have to override that class in our own Local Module.

For that we need to create our own module and to make the changes in local module

config.xml file, and some changes in our own module Model class.

 

To override the Model classes we have to add <rewrite></rewrite> tag under module name.

Let us take an example that we need to override 

Mage_Sales_Model_Quote_Address

Mage_Sales_Model_Quote

Mage_Sales_Model_Quote_Item_Option  classes.

then below is the code we need to write in our config file under global node.

 

Our own module name is Hello module and in we need to write the code at below path

 app/code/core/local/Hello/Sales/etc/config.xml

 For overriding Mage_Sales_Model_Quote class

 

<config>

 <global>

      <models>

              <sales>

                    <rewrite>

                          <quote>Hello_Sales_Model_Quote</quote>

                   </rewrite>

             </sales>

     </models>

 </global> 

</config>

 

For overriding Mage_Sales_Model_Quote_Address class

 <config>

     <global>

           <models>

                   <sales_quote>

                          <rewrite>

                                <address>Hello_Sales_Model_Quote_Address</address>

                         </rewrite>

                  </sales_quote>

          </models>

   </global> 

</config>

 

For overriding Mage_Sales_Model_Quote_Item_Option class

 <config>

        <global>

               <models>

                       <sales_quote>

                              <rewrite>

                                    <item_option>Hello_Sales_Model_Quote_Item_Option</item_option>

                            < /rewrite>

                     </sales_quote>

              </models>

     </global> 

</config>

 

 And as per the core class path we have to create the below files in our module

 app/code/core/local/Hello/Sales/Model/Quote.php,

 app/code/core/local/Hello/Sales/Model/Quote/Address.php,

 and app/code/core/local/Hello/Sales/Model/Item/Option.php

 

In these file the code will be

 

class Hello_Sales_Model_Quote extends Mage_Sales_Model_Quote

{

  // module code according to requirment

}

 

class Hello_Sales_Model_Quote_Address extends Mage_Sales_Model_Quote_Address

{

  // module code according to requirment

}

 

class Hello_Sales_Model_Quote_Item_Option extends Mage_Sales_Model_Quote_Item_Option

{

  // module code according to requirment

}

 Now magento will read our code existing in local folder  before the core folder  according

to magento code pool standard.