How to override Magento model classes?

Featured Image

Many times we need to implement new functionality of existing Magento core classes, but we don’t want to modify core classes.
For controllers, that’s quite easy. Usually we can copy controller from core and put it in the same place in local directory, modify class and that’s it.

Of course, there are better ways to do that (make module), but I’m not going to write about that right now since it is not subject of this post.

I only mentioned that because we can’t do same thing for models even if we want to, so how to override Magento models without modifying core files?

Fortunately, that’s very easy =)
Let’s make one extension of that kind!

Extension we are going to  make will be useless, only difference between this extension and original class will be  class name var_dump =)

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.

Call it Inchoo_Wishlist.

Create app/code/local/Inchoo/Wishlist/model/ directory and copy app/code/core/Mage/Wishlist/Model/Item.php
there.

Now,  rename class Mage_Wishlist_Model_Item to Inchoo_Wishlist_Model_Item.

Add this line:

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

in loadByProductWishlist method

Now, let’s create Inchoo_Wishlist.xml in app/etc/modules/
Put this code there:

< ?xml version="1.0"?>
<config>
    <modules>
        <inchoo_wishlist>
            <active>true</active>
            <codepool>local</codepool>
        </inchoo_wishlist>
    </modules>
</config>

Now, make app/code/local/Inchoo/Wishlist/etc/Config.xml file and put this code there:

< ?xml version="1.0"?>
<config>
    <modules>
        <inchoo_wishlist>
            <version>0.1</version>
        </inchoo_wishlist>
    </modules>
    <global>
       <models>
          <wishlist>
              <rewrite>
                  <item>Inchoo_Wishlist_Model_Item</item>
              </rewrite>
          </wishlist>
       </models>
    </global>
</config>

You are done! Try to add something to wishlist! =)
Play with other models as well!

Cheers ^_^

15
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Tomas Novoselic

Team Leader / Senior Developer

At Inchoo, Tomas is a Team leader and Certified Magento Developer. He handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Other posts from this author

Discussion 15 Comments

Add Comment
  1. m4e

    You should use instead of :)

  2. m4e

    // lost tags, sorry
    You should use “codePool” instead of “codepool”
    Magento is case sencitive, you know

  3. @ m4e Tnx for mentioning that! I’m not sure what happened, maybe this code display plugin for wordpress. I’ll check that asap

  4. Hm… it seems that I was right, wordpress plugin turns all tags to small case :(

  5. Michael

    Thanks for the effort.

    Seems like Branko got it covered already at ActiveCodeline, Overrider module, which shows it nicely how to do it ( http://activecodeline.com/modules/ ).

  6. Vahagn

    Please correct xml tags there is a space between opening tag and exclamation sign: “< ?xml"

  7. Jamal

    Does anybody know how to override this magento class

    class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
    { }

    In my config.xml file, I have made the following change:

    …..

    MyCompany_MyModule_Model_Currency

    ……

    But it is not working! I’m missing something?

    Regards,

  8. Worth to mention is that if the class you want to override has a deeper catalog structure then you need to provide the whole path to the element in the config.xml file.

    For example, I had to override the password length check in

    class

    Mage_Customer_Model_Customer_Attribute_Backend_Password

    My config file:

    *snip*

    MyCompany_Customer_Model_Customer_Attribute_Backend_Password

    *snip*

  9. dipali

    Can you pls guide how to implement tax deduction in cart for uk ecommerce website,

    we need to have cart like this
    e.g.
    Subtotal £270.50 (Note: product prices in a site are tax inclusive)
    tax -£40.29 ((after deduction £230.21) Note: calculation should be done like http://www.digita.com/taxcentral/home/calculators/vatcalculator/default.asp it’s done her for 17.5%(tick) Inclusive of VAT, also, kindly notice that i want to deduct tax for some countries and for some it won’t be deducted . I’ve csv ready for the same. )
    delievery charge £18.00
    Final Total £248.21

    Can you pls guide how can i apply above like tax imple in magento?

    can i get any plugin ? or orverrride fuction fetch in model class , how?

  10. Rob

    Looks quite simple, but how overwrite a model which is 1 folder deeper?

    I tried this without any results. What am I missing?

    app/code/local/Personal/Checkout/etc/config.xml

    0.0.1

    Personal_Checkout_Model_Type_Multishipping_State
    `

  11. Kamal

    Thx guys, 100% working

  12. saumil

    i am new in magento your blog makes very helpful to me for learn magento

  13. eggsplanet(gowri)

    How can i sort the grid by wishlist.please help me.detailed question is here

    http://stackoverflow.com/questions/7683653/custom-sort-list-in-magento-grid

  14. Sam

    I’m curious why you wouldn’t just copy the app/code/core/Mage/Wishlist/Model/Item.php to app/code/local/Mage/Wishlist/Model/Item.php and make your changes there? I currently override the Mage/Core/Model/Layout/Update.php file this way.

    You’re using the full original Item.php file contents anyways, so if that’s updated at some point you’ll still need to update your override also. So what’s the advantage in going this route?

  15. Here is the CheatSheet for Rewriting Block/Helper/Model class (taken from CheatSheet developed by Vinai):

    <config>
    	<global>
    		<models> [1]
    			<sales> [2]
    				<rewrite>
    					<order_item>Namespace_Module_Model_Sales_Order_Item</order_item> [3]
    				</rewrite>
    			</sales>
    		</models>
    	</global>
    </config>

    [1] Depending on object type “models”, “helpers” or “blocks”
    [2] Module shorthand
    [3] The tag is the class to override, the content is the full name of the replacement class.

    You can download the cheat-sheet from:
    http://www.blog.magepsycho.com/magento-cheatsheet/

    Cheers!

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top