«Back to blog home

Magento Therapy: Overriding Core Magento Files

Let's pretend your Magento store needs to modify core functionality offered inside of the app/code/core/ folder. I'd have to smack you if you went modifying those files directly as it wrecks your ability to easily maintain changes throughout upgrades. Fortunately, Varien was good enough to know that the way they developed Magento isn't the way everyone may need it to work. Barely on the surface, you have three code folders: community, core and local. In this post, I'll be reviewing how to rewrite and extend those classes inside of the core folder.

Extending Core Classes

As mentioned in my Multi-Store and Internationalization post, PRPL adopted an inverse Translate method that fetches from translate.csv files before those located in app/locale/. In order to do so, we needed to make this change global while keeping the scope of impact as minimal as possible.

Magento manages their dependency injection through the XML configuration files. When you want to rewrite a class, you can tell Magento to look elsewhere from where it normally would.

Inside of app/code/local/, we have a namespaced folder called Prpl, which is where we'll be putting any of our custom code1. Place the following code inside of app/code/local/Prpl/Core/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Prpl_Core>
            <version>0.1.0</version>
        </Prpl_Core>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>
                    <translate>Prpl_Core_Model_Translate</translate>
                </rewrite>
            </core>
        </models>
    </global>
</config>

It should be pretty obvious what's going on here: We're telling Magento to rewrite the "core/translate" model to our class. The cool part about overriding Magento classes in this way is that we can extend them2. Now to create the class, Prpl_Core_Model_Translate, with the following contents:

class Prpl_Core_Model_Translate extends Mage_Core_Model_Translate
{
    /**
     * Reverses the priority of translations to check translate.csv first, and
     * then the locale packages second -- the way it should be.
     *
     * @param text $text
     * @param text $code
     * @return text
     */
    protected function _getTranslatedString($text, $code)
    {
        $translated = '';

        if (array_key_exists($text, $this->getData())) {
            $translated = $this->_data[$text];
        } elseif (array_key_exists($code, $this->getData())) {
            $translated = $this->_data[$code];
        } else {
            $translated = $text;
        }
        return $translated;
    }
}

Enabling Your Namespace

Lastly, lets not forget to enable your new module. We prefer using a module xml file called Prpl_All.xml, which is placed at app/etc/modules/. The basic contents would look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <Prpl_Core>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Prpl_Core>
    </modules>
</config>

After that, you should be all wrapped up and ready to go. If caching is enabled, you'll likely have to clean it out.

Magentoisms

Today I was working on a new module for one of our clients for some enhanced inter-product support functionality. Since it's largely focused towards live search functionality, I figured I would piggyback some of the CatalogSearch functionality to reduce duplicate code.

In doing so, I ran into a small roadblock in that it appears overwriting controllers isn't as auto-awesome in Magento as with models and other classes. I didn't investigate far into the "why", but the Magento autoloader wasn't playing nicely.

The point I'm making with this is that little things like this are bound to crop up and they're hurdles you simply have to expect. Keep special attention on the XML files: they have been known to be sensitive to whitespace. When this happens, Magento doesn't give you any considerably useful errors. *face palm*

Foot Notes

  • 1 If you're creating a namespace for your company to be placed in multiple client projects, I would recommend plopping it in the community folder. I prefer having the ability to have a common code base that we can then extend further in local for the client if necessary.
  • 2 The other method of rewriting Magento classes, which I don't recommend, is to copy the entire contents of the class you want to overwrite into the local folder and put it inside of a mirror file (e.g. Mage/Core/Model/Translate.php). Doing it this way totally blows for two primary reasons: 1) you need to maintain more code and 2) if Varien updates Magento, see point 1. This is the method I used prior to discovering XML rewrites... those were dark, dark days. It's a total pain in the ass and wreaks of code smell. I can't think of any particularly enticing reasons to use this approach.

 

Comments

Mark (not verified) says:

Thanks! Spent ages trying to figure out why my overrides weren't working correctly. I had MyCompanyName_All.xml in my /app/etc/modules/ directory but this wasn't working correctly. So I added one for each namespace I was extending and it works a treat.

Bravo.

Anonymous (not verified) says:

Do you think this method could be used to override the main header banner on homepage.

Instead of creating a whole new design to reference?

Anonymous (not verified) says:

Do you also know how to overload a class inside the "Controller" folders? For example Mage_Core_Controller_Varien_Front?

Brian Carlson (not verified) says:

I thought this post was going to be about the X-Men.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

rob Zienert

I was blessed with being born into a tech-savvy family: my dad being a vehicle engineer at Ford and my mom a database programmer — it only made sense that I put my genetic inheritance to good use, so here I am. Whether it be application architecture, code reviews, or any flavor of voodoo-techno-whizbang; its my job to make sure your application or web site functions technically.

Outside of the office, I have the pleasure of serving on the Program Advisory Committee for Full Sail University’s Web Design and Development Bachelor Program. But alas, not all I do is work, oh-no. I’m super passionate about fast, sexy cars (and bikes), painting, coffee, watching the History channel, and playing paintball. I also play a lot of Modern Warfare 2 on XBox (Gamertag: Pievendor).

Oh! How could I forget: My nickname around town is RZA, like the rapper, because I’m so convincingly street.

my Favorites

Paintball

Used to fly around the nation to shoot people. (Anyone need a D2 player?)

Painting

I might be a programmer by day, but I love digital painting--even if its always sad and emotive.

Radiohead

Constantly ridiculed by my fellow co-workers for how much I love Radiohead... but no one can touch them.

my Last·fm

  • Meridional
  • Inception
  • Kurr
  • The Five Ghosts

my Flickr

  • Happy Birthday Justin!!
  • Photo 5
  • Foosball Fights
  • Foosball Fights