Third & GroveThird & Grove
Feb 6, 2014 - Nathan Bennett

Extending Magento Core Classes

No code base is ever one size fits all. It’s a rare case where you install a software platform (such as Magento) and the client is overjoyed from day one, and you never have to touch the code base again. Sounds dreamy, but honestly, kind of boring. One of the most exciting and challenging parts about engineering is customizing code to implement new functionality for clients. But how do you do it?

With Magento, one of the ways we implement new functionality is to extend core classes. This is helpful when you need to overload a method or create a new method for a core class, but how do you do it without changing the core class itself? If you change the core class, when it’s time to upgrade to a new version of Magento, you face the daunting challenge of remembering where you made changes to the core and importing those changes into the new code base, which can cause a ton of headaches. This is the ‘core’ reason for the standard rule: don’t change core code!

So now you’re in trouble. You need to modify a core model (or controller, or helper) class but want to keep the ability to upgrade Magento, how do you do it? The standard practice is to copy the file from the /app/code/core/Mage directory into your /app/code/local/Mage directory, and make modifications there. The new file with your new code will be picked up by the autoloader and all is well. But what happens when you upgrade Magento and there are new or modified methods in the core file? The autoloader is still going to pick up your file in /app/code/local/Mage, and you lose out on any new features that might be included with the upgrade.

The solution is to create a new module, include the core file, extend it in the code, and use a rewrite in the configuration xml to tell Magento to use the new module code rather than the core. It’s a bit round-a-bout, but allows core classes to be updated while retaining your custom functionality.

Let’s say we want to modify /app/code/core/Mage/Catalog/Model/Product.php, and add a new method called makeBlue. We’d make a new module called Companyname in /app/code/local/. Within the module we’ll make a file to mirror the Mage file: /app/code/local/Companyname/Catalog/Model/Product.php. Then we create a configuration.xml: /app/code/local/Companyname/etc/config.xml. Within the config.xml file, we tell Magento that we want to rewrite the core Catalog Product Model with our new Companyname_Catalog_Model_Product class:

<?xml version="1.0"?>
<config>
    <modules>
        <Companyname_Catalog>
            <version>1.0.1</version>
        </Companyname_Catalog>
    </modules>
    <global>
       <models>
        <catalog>
          <rewrite>
            <product>Companyname_Catalog_Model_Product</product>
          </rewrite>
        </catalog>
       </models>
    </global>
</config>

 

Once the rewrite is in place, we need to add an include to our new /app/code/local/Companyname/Catalog/Product.php class file to include the core Product class:

include('Mage/Catalog/Model/Product.php');

 

And finally, we can extend our new module Product class from the Mage core class, and add our new makeBlue method:

class Companyname_Catalog_Model_Product extends Mage_Catalog_Model_Product {
  public function makeBlue() {
    // serious code magic to make the product blue
  }   }

 

And voila! Now we have a custom class, extending the core, which will be preserved through a Magento upgrade!

(Just don’t forget to tell Magento about your new module in /app/etc/modules/Companyname.xml.)