Code Guru

Code Guru

By James Seavers

Wednesday, 22 August 2012

Customising Magento the Right Way

Customising Magento the Right Way

If you use Magento, chances are you will need to customise the way some of the core features work. 

Magento is very flexible, I don't want to say too flexible, that would be silly. But there is more than one way to override the core classes, the easy way, and the right way. I'm going to show you why you should take a little more time, and do it correctly.

First up, the easy way

So you tracked down the pesky class that's causing you grief in Magento, chances are you wound up somewhere within the app/code/core/Mage folder. Don't even think about amending a core file here bandito, you don't want to think about the grief pulling that kind of stunt can land you. Malas noticias amigo.

Not me señor, I don't go round amending core files, I have my good coding badge. So you copy the said file so that it resides in the local folder, so Magento knows to override the core file with this one.

Boom, save it to app/code/local and mirror the rest of the directory structure from the core and voila job done, sit back and roll a burrito with a smug grin under your huge moustachio.

But wait, we've just copied an entire class and overridden it, chances are we are making a small change to one function, doesn't that seem a little foobar? Of course it does, because it is!

What happens if there's a security vulnerability identified later and patched, or some new fandango functionality released at a later date that will live it that exact class. You'll either be caught with your pants down, or stood there wondering why you are getting left behind. No so smug now amigo?

The Right Way

This takes slightly more effort, but it will be worth it. We can extend any core class with a custom module of our own. Doing it this way, we can retain the functionality of the core class, and extend any or all of it’s methods. So, if the core class gets amended due to security or it has some new functionality, our newly created extended class can take advantage of the new code, automatically.

Note the use of ‘extend’ and not ‘override’. This is an important distinction. Allot of forum posts will show you how to override a class, but extending a class is a far more efficient and scalable way to go about things. Thinking about it in say food terms, it’s like having a class called ‘Taco’. Now lets say I like sour cream and jalepeno’s on my Taco, I could simply extend my Taco_Class, and add in a method that adds the necessary creamy/fiery goodness I crave when eating a Taco.

Here’s what needs to happen for Magento Custom Module happiness chicas and muchachas:

1. Register Your Custom Module

Tell Magento about your new custom module by creating a new file in app/etc/modules called Yournamepsace_Yourmodule.xml.

Note, replace ‘Yournamespace’ with a name that makes sense to you, generally it’s your company or organistion name, ours is Symphony. Now replace ‘Yourmodule’ with a name that makes sense for the module your are creating. If you were extending the navigation model, ‘Navigation’ would be a good choice. Using that logic, our example file would be:

app/etc/modules/Symphony_Navigation.xml

2. Register your Local Override

We’ve register our custom module, so Magento will look for it with the app/local directory where we keep all our custom modules. So, let’s create a config file that will tell Magento about our custom module, and which class or classes we are going to extend.

In app/local/Symphony/Navigation/etc create a file called config.xml. Note the path, etc is where Magento looks for config files, simple eh?

In the config.xml, put the following:

<?xml version="1.0"?>
<config>
<global>
   <blocks>
       <catalog>
           <rewrite>
               <navigation>Symphony_Navigation_Block_Navigation</navigation>
           </rewrite>
       </catalog>
   </blocks>
</global>
</config>

So what is going on here?  Simple, we are telling Magento that we need to rewrite the class for our navigation. The class in question is a block, think view, and is delivered by the core Catalog module. 

If you want to understand the structure of a Magento config.xml, here's a great tutorial http://www.ecomdev.org/2010/08/31/magento-module-configuration-file-reference.html.

3. Mirror the core structure in our local module and extend the class

Next up, recreate the original core file structure in your local module, app/local/Symphony/Navigation/Block/Navigation.php. Extend the class like so:

class Symphony_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation

and carry out the necessary modifications to the method you need to alter. Don’t copy the entire file, just add in the method or method that you need to amend. Here's my code to disable the cache for the navigation block so we can add in some navigation items, as an example:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to This email address is being protected from spambots. You need JavaScript enabled to view it. so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/**
 * Catalog navigation
 *
 * @category   Symphony
 * @package    Symphony_Navigation
 * @author     Symphony Online <www.symphonyonline.co.uk>
 */
class Symphony_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation 
{
    protected function _construct()
    {
        //$this->addData(array(
            //'cache_lifetime'    => false,
            //'cache_tags'        => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG),
        //));
    }
}

I've commented out the cache stuff, I could have also deleted it.

4. Clear the cache

Vamos! Any bandito worth his salt and tequilla knows to always, always, always clear the Magento cache to put the changes into full effect!

You can check your shiny new custom module is working and enabled by going to:

Admin > System > Configuration > Advanced and checking that your Yournamespace_Yourmodule is listed and enabled.

That’s it for this tutorial, buenas dias amigios!

Comments (5)

  • gucci財布

    24 August 2012 at 04:26 |
    m impressed! Extremely useful information specifically the last part I care for such info a lot. I was looking for this certain info for a long time. Thank you and good luck.
    • Code Guru

      24 August 2012 at 07:57 |
      Glad to be of help amigo, watch out for more tasty Magento tips coming soon!
  • Website Design Cardiff

    07 September 2012 at 09:20 |
    Nice lil guide, am yet to do a e-commerce site, would you say magento is good all rounder?

    -Alex.
    • Code Guru

      07 September 2012 at 10:40 |
      Alex - yes, Magento is well regarded by php developers due to a number of good reasons:

      1. It's fully featured - the core functionality is very comprehensive (catalog management, promotions, api).

      2. It's got a free option :)

      3. This is the most important aspect, it's written in an MVC type way, and is based upon the Zend Framework. This means it's very customisable, and extenable.

      I would say it's MVC architecture and use of the Zend Framework make it what makes it stand out - lots of other ecommerce systems are written in a very procedural way, would say that is allot harder to develop with and doesn't have the benefits that comes with a framwework and PHP5.

      On the down side, it can be really tricky to set up and migrate, particularly with an existing site. Thinks days and not minutes sometimes!

Leave a comment

You are commenting as guest.

Search by Date

Latest Big Bad Blog Post

ExpressionEngine Force HTTPS For Selected Sections with Htaccess

ExpressionEngine Force HTTPS For Selected Sections with Htaccess

Have you ever wanted to force a HTTPS connection for your ExpressionEngine website? If so, you may have also wanted to 'unforce' that connection for other areas of your website, either for performance issues or because maintaining both HTTP and HTTPS for the same section can be tricky. Avoid those pesky browser warnings about unsafe (non HTTPS) assets with this htaccess rewrite.

Continue Reading

Latest twitterings

  • @reverseRett proud Daddy and Uncle!!xx-- Delivered by Feed43 service

    27 May 2014 | 11:40 am View full tweet
  • Check out "Internet Archive" on Vimeo http://vimeo.com/59207751  #Vimeo-- Delivered by Feed43 service

    24 May 2014 | 1:03 pm View full tweet

Fill in our contact form

  • Please complete all the fields

Thanks for your message! We will get back to you ASAP!

* indicates a required field

TOP