How To Overwrite or Add To Magento Core Files

There are some situations when you may need to change the way Magento core files work. But you must do it the right way, you can’t just start editing files in app/code/core/.

Why shouldn’t you edit core files directly?

To put it in simple terms: so you can install updates. If you change the core files directly, and then at some future date decide to install a Magneto update the you run the risk of losing all of the changes that you made as the core files are completely overwritten by updates.

So what is the right way to change core files?

The correct way to make changes to core files is to overwrite the parts that you want to change and put it in your local codebase in

app/code/local/

(or in the case of community extensions, it would go in

app/code/community/

).

A Step by step example using the

Mage_Sales_Model_Quote

class in Magento’s core

  1. Locate the file and copy the file path into your local directory.

    In our example, the location of our file in the core is

    app/code/core/Mage/Sales/Model/Quote.php

    . We need to copy everything after the core directory, that would be

    Mage/Sales/Model/Quote.php

    , and build the same structure in our local namespace directory. If our local namespace is “Mywebsite” then our new file path would be

    app/code/local/Mywebsite/Mage/Sales/Model/Quote.php

    .

  2. Create a class to extends the core class.

    In our newly created file, we need to create a class that extends the core class. Just like all Magento classes, our new class name needs to match the file path. Since our file path is

    app/code/local/Mywebsite/Mage/Sales/Model/Quote.php

    then our new class name would be

    Mywebsite_Mage_Sales_Model_Quote

    . Our new class must extend the core class we want to change, it should look something like this:

    class Mywebsite_Mage_Sales_Model_Quote extends Mage_Sales_Model_Quote {
       //do something here
    }
    
  3. Write your changes in the new class.

    Now that you have the new class, you can add a new method or change an existing method. In our case, we want to change the

    setHasError()

    method in the core class so that it logs an exception whenever an error is set on a quote. In order to do that, we will start by copying the original method and pasting it in our new file. Once it is in our new file, we will replace the code inside the method with

    parent::

    followed by the method we are changing, be sure to include any parameters. Doing this will ensure the the original method in the core class will still run as intended. Our class now looks like this:

    class Mywebsite_Mage_Sales_Model_Quote extends Mage_Sales_Model_Quote {
    
        public function setHasError($flag)
        {
            parent::setHasError($flag);
        }
    }
    

    Now we are going to add our custom code that will throw the exception that we want before it runs the original method:

    class Mywebsite_Mage_Sales_Model_Quote extends Mage_Sales_Model_Quote {
    
        public function setHasError($flag)
        {
            try {
                if ($flag) {
                    Mage::throwException(Mage::helper('queue')->__('Quote '.$this->getId().' Has Error Exception'));
                }
            } catch (Exception $e) {
                Mage::logException($e);
            }
            parent::setHasError($flag);
        }
    }
    
  4. Create a config file for your new class

    We are almost done! Just one more important part to go: we have to tell Magento about our new class. To do that we need to create a

    config.xml

    in the

    app/code/local/Mywebsite/Mage/etc/

    directory.

    In this new file we need to put the following lines:

    <config>
        <global>
            <models>
                <sales>
                    <rewrite>
                        <quote>Mywebsite_Mage_Sales_Model_Quote</quote>
                    </rewrite>
                </sales>
            </models>
        </global>
    </config>
    

    This tells Magento that we want to use our new class instead of the one in the core files. Our changes won’t work without this step.

  5. Clear the cache and test the changes

    Now we just need to clear Magento’s cache and if we did everything right then our changes should start working!

Leave a Reply