Blog

Override Magento Core Model Classes – Magento Customization

Posted Sep 22nd, 2009 at 11:58 PM and seen 887 times

magento_ecommerce
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:

1
var_dump(get_class($this)); exit();

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”

  1. New Says:

    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

  2. priya Says:

    I have tried override wishlist class but it's giving me an error for cache. Please help. Thanks in advance

  3. priya Says:

    I have tried override wishlist class but it's giving me an error for cache. Please help. Thanks in advance

  4. MagentoMagik Says:

    You have to check the code, if the class is overridden properly it should not give an error rather work as per new logic.

Leave a Reply

blog comments powered by Disqus

  • Recent Posts

Monthly Archives

Popular Tags