Blog

You must have noticed that sometimes you can’t use default Magento Models and require changes due to certain business logic you are about to follow. These changes are done to implement new functionality which are not part of existing Magento Installation. Overriding Controller is not difficult as you can copy the code from core in controller and keep the file in local directory. Now you can edit the class as per your requirement which will pretty much do the trick. You might know better and efficient way of doing this but that’s not a point here. I wanted to elaborate on how to override default Magento’s model class without even changing the core files. Tricky isn’t it?
In this article I am going to write a short code which will help you in overriding default Magento model without changing its core files. We have to create an extension which will do the trick. Simple difference between this extension and original class will be class name var_dump.
Now, we will choose any model class from core of Magento, in our case we will choose Mage_Wishlist_Model_Item which you can locate under app/code/core/Wishlist/Model/Item.php
In order to add new functionality in this class we will create a new module. What we are going to do now is to take a random model class from Magento core.
Let it be Mage_Wishlist_Model_Item located in app/code/core/Mage/Wishlist/Model/Item.php
What we want to do is to add new functionality to that class, so let’s make new module called as MagentoMagik_Wishlist (you can change the name as per your requirement)
Create app/code/local/MagentoMagik/Wishlist/model/ directory and copy app/code/core/Mage/Wishlist/Model/Item.php in that particular location.
Let’s rename class Mage_Wishlist_Model_Item to MagentoMagik_Wishlist_Model_Item.
Now, we have to add the line given below:
in loadByProductWishlist method
Now, let’s create MagentoMagik_Wishlist.xml in app/etc/modules/
and write the following code it in:
1 2 3 4 5 6 7 8 9 | < ?xml version="1.0"?> <config> <modules> <magentomagik_wishlist> <active>true</active> <codepool>local</codepool> </magentomagik_wishlist> </modules> </config> |
Now, create app/code/local/MagentoMagik/Wishlist/etc/Config.xml file and write the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < ?xml version="1.0"?> <config> <modules> <magentomagik_wishlist> <version>0.1</version> </magentomagik_wishlist> </modules> <global> <models> <wishlist> <rewrite> <item>MagentoMagik_Wishlist_Model_Item</item> </rewrite> </wishlist> </models> </global> </config> |
This is merely an example of overriding the default Magento’s Model files and you can change the other model in similar way. All you have to do is plan what change you require and don’t hurt any Model which is required by Magento. I hope this example must have helped you in changing the default behavior. Please leave me a comment and share your experiences, problems with us.
View Comments to “Override Magento Core Model Classes – Magento Customization”
Leave a Reply
- Recent Posts
- September 2010 (1)
- July 2010 (8)
- June 2010 (1)
- March 2010 (1)
- February 2010 (2)
- January 2010 (10)
- November 2009 (1)
- October 2009 (1)
- September 2009 (3)
- August 2009 (5)


January 3rd, 2010 at 12:53 AM
Hi,
I want to add a new menu list”Add Gifts” in SYSTEM>CONFIGURATION>SALES>SALES.
how can I do that?
Please suggest
Thanks in advance
January 20th, 2010 at 10:22 PM
I have tried override wishlist class but it's giving me an error for cache. Please help. Thanks in advance
January 21st, 2010 at 6:22 AM
I have tried override wishlist class but it's giving me an error for cache. Please help. Thanks in advance
July 5th, 2010 at 7:15 AM
You have to check the code, if the class is overridden properly it should not give an error rather work as per new logic.