+91 796-525-5075 ( 11AM To 7PM-IST )
Hr@logicrays.com
Biz@logicrays.com
 

Overriding Model In Magento

Magento has feature to support Extensibility and scalability like other framework in the core itself, which will allows programmers to create custom extensions to fulfill their requirements, thus providing Extensibility. It also allows to override core files to change the flow and behavior of the core structure of Magento.

Many times we need to add new functionality of existing Magento core model classes, but we cann’t modify core classes, because if we upgrade magento in future time then our changes will gone away.So better to avoid changing the core files.

Suppose we have a module called Lr_Custom present at local directory app/code/local/Lr/Custom and we want rewrite below model classes

Overriding Model

Suppose we want to rewrite the method setRequest of class Mage_Usa_Model_Shipping_Carrier_Fedex,then we need these below files

  • app/etc/modules/Lr_Custom.xml : It’s a file used to enable our custom module.
  • app/code/local/Lr/Custom/etc/config.xml : It’s a module configuration file in which we’ll set up Model class overriding using certain tags as per the Magento conventions.
  • app/code/local/Lr/Custom/Model/Usa/Shipping/Carrier/Fedex.php : It’s a custom model class file which we’ll use to override the core “Usa” model class.

and we can rewrite by below code in config.xml file.

Explanation:

we can divide the class Mage_Usa_Model_Shipping_Carrier_Fedex into different parts like

  • Mage_Usa_Model
  • Shipping_Carrier_Fedex

How they compared to our rewrite logic ?
if you see the config.xml in dir(app/code/core/Mage/Usa/etc/config.xml) file of Mage_Usa module then you will be getting below code

From above code what we got that “usa” defines Mage_Usa_Model.

here we got “Mage_Usa_Model” by “usa“, and for whole class “Mage_Usa_Model_Shipping_Carrier_Fedex” by “usa/shipping_carrier_fedex

finally your file will be at Lr_Custom_Model_Usa_Shipping_Carrier_Fedex means
app/code/local/Lr/Custom/Model/Usa/Shipping/Carrier/Fedex.php

class destination would be

Finally our module files will be

Declaration:
dir: app/etc/modules/Lr_Custom.xml

Configuration:
dir: app/code/local/Lr/Custom/etc/config.xml

Rewrite Class:
app/code/local/Lr/Custom/Model/Usa/Shipping/Carrier/Fedex.php

In the same if we do assignment to rewrite loadByCustomer method of class Mage_Newsletter_Model_Subscriber then our files will be

Declaration:
dir: app/etc/modules/Lr_Custom.xml

Configuration:
dir: app/code/local/Lr/Custom/etc/config.xml

Rewrite Class:
app/code/local/Lr/Custom/Model/Newsletter/Subscriber.php