Magento – Extending a module controller

controllersextensionsmagento-1.9module

essentially, I have a purchased a module to allow VAT Exemption, the user fills out a form as a step in the one page checkout.

The plugin is pretty basic and only contains a couple of fields, which I need more such as Charity Address and other information.

I dont want to overwrite the modules core files as this is bad practice, I am looking to extend it so that I can add additional fields to the save method.

Any pointers? I have been through a few tutorials on creating another module to extend the module's controller but for some reason no matter what I do I get no feedback at all.

All the tutorials I have seen all seem to extend the core controllers, which is pretty basic but doesn't seem to apply to my situation.

How do I go about this in Magento CE 1.9

Edit: Below is my current code for the my custom module that should overwrite another module, where is this going wrong?

etc/module/MyName_VatExemptExtended.xml

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

local/MyName/VatExemptExtended/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MyName_VatExemptExtended>
            <version>0.0.1</version>
        </MyName_VatExemptExtended>
    </modules>

    <frontend>
        <routers>
            <vatexempt>
                <args>
                    <use>standard</use>
                    <modules>
                        <MyName_VatExemptExtended before="Indies_Vatexempt_OnepageController">MyName_VatExemptExtended_OnepageController</MyName_VatExemptExtended>
                    </modules>
                </args>
            </vatexempt>
        </routers>
    </frontend>
</config>

local/MyName/VatExemptExtended/controllers/OnepageController.php

<?php

require_once Mage::getModuleDir('controllers','Indies_Vatexempt_OnepageController').DS.Adminhtml.DS.'OnepageController.php';

echo "DD";

class GiantPeach_VatExemptExtended_OnepageController extends Indies_Vatexempt_OnepageController

{
    public function __construct() 
    {
        echo "__construct";
        die();
    }

    public function saveExemptAction()
    {
        echo "saveExemptAction";
        die();
    }
}

As you can see, at the moment I just want it to display something from my extended controller. I can see my custom module appearing in the Magento backend so it's loading the module ok, however, in my config if I force an error (by unclosing a tag or writing some rubbish outside of tags) I don't receive any visible syntax errors. Is my module being loaded correctly?

Thanks

Best Answer

Extending a Module is done in the same way as you would with core. If it is simply changing the nature you can override the class For Example : My_Mod_Model_Sales_Order extends There_Mod_Model_Sales_Order

Then just use the same method call so that your method is called instead of the purchased Mod.

You will to create a directory structure for you mod in "Local". The key is to ensure you have a rewrite in your config.xml

See http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento#9 For an good description of the reasons for the layout (He describes the logic behind the search that the code is doing) and the rest of the series for the good intro to the whole area.

For Example:

<global>
    <models>
        <sales>
            <rewrite>
                <order>My_Mod_Model_Sales_Order</order>
            </rewrite>
        </sales>
    </models>
</global>

If you can use an observer to capture events and to inject new code/data it is easier to manage and does not have the issue of other code stepping on your toes.

For controllers you need to do a little more work. For an example here is an overriding the CartController (to fix the problem of a case sensitive coupon...)

require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'CartController.php');
class My_Checkout_CartController extends Mage_Checkout_CartController 
{
    public function couponPostAction()
    {.......}
}

And the associated config.xml

<config>
    <modules>
        <My_Checkout>
            <version>1.0.0</version>
        </My_Checkout>
    </modules>
    <frontend>
      <routers>
        <checkout>
          <use>standard</use>
          <args>
            <modules>
                <my_checkout before="Mage_Checkout">My_Checkout</my_checkout>
            </modules>
          </args>
        </checkout>
      </routers>
    </frontend>
</config>
Related Topic