Overwrite Magento checkout controller route

Posted by | February 17, 2012 | Magento, PHP | No Comments

I’v been playing with Magento for a while and yes! there are times when I love it and there times when I wish that it hadn’t be borned. I remember first time when I had to inject some new functionality over the onepage checkout controller. Doing it for the fist time is really painful but after you get the trick it’s really simple.

So first step you have to do is to touch the etc/config.xml local module configuration file (I suppose that you already have set a module in the app/code/local/Namespace/Module) on the routes section and add the following lines of code:

 <routers>
     <checkout>
       <args>
        <modules>
        <!-- Overwrite onepage checkout controller -->
        <Namespace_Module before="Mage_Checkout">Namespace_Module </Namespace_Module>
        </modules>
      </args>
     </checkout>
 
  </routers>


In the XML configuration you have informed Magento that the Mage_Checkout module route is overwritten by our Namespace_Module module. Now we only have to extend the Checkout controller and add our functionality, so create the app/code/local/Namespace/Module/controllers/OnepageController.php with the following content:

//You need to include the extended controller by hand, the auto-load will not get it for you this time 
require_once 'Mage/Checkout/controllers/OnepageController.php';
 
class Namespace_Module_OnepageController extends Mage_Checkout_OnepageController {
 
    /**
     * Index action 
     */
    public function indexAction() {
       //TODO add functionality 
       parent::indexAction();
    }
}

Now you’we got the trick, go ahead and be wild !! 🙂

About Andrei Tara

Andrei is a developer/geek who loves creating high quality and efficient applications, always following the best practices in software engineering. He loves the challenge of learning new, exciting things and playing with new languages, frameworks, and tools.

Leave a Reply