Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override a Magento controller?

I need to check the validity of a coupon code on the checkout/cart page with server-side code.

Magento already ships with a similar check in place. However, I need to add one to see if a user is connected or not: what would be the best way to extend/override that action in Magento?

I know I can copy the controller PHP file to the /app/code/local/ folder tree, but I'm wondering whether there's a better way to do it.

like image 513
JohnT Avatar asked Jun 16 '11 15:06

JohnT


People also ask

How do you override a controller?

To override a controller, you have to create a subclass to define existing functions. Now select a function from the controller for overriding from the addons. For better understanding, I have selected the following Carousel Recently viewed function for overriding.

How do I override a controller in Magento 1?

If we want to override controller file in magento 1 then we need to add create custom modules like following: Create a new module and declare it in “app/etc/modules” by adding a xml file.

How do I override a module in Magento 2?

Override module view-files in the app/code folder. Make the necessary changes to the newly created theme file. If your own theme is missing from your Magento installation, create new theme.

How do I override a Magento 2 template?

More specifically, in Magento 2, any module's or parent theme's layout, template, or web can be overridden with ease just by placing it in <theme_dir>/<Vendor>_<Module>/path/to/file . For instance, for the Magento_Theme module, you can place your template in <theme_dir>/Magento_Theme/templates/html/header.


1 Answers

Anything besides modifying the core is good in my opinion. With that said create a simple module with a controllers directory and etc with config.xml:

<config>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                         <My_Module before="Mage_Checkout">My_Module_Checkout</My_Module>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

See here for more details on how to extend the frontend controller: http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/

like image 123
B00MER Avatar answered Oct 06 '22 00:10

B00MER