How to Extend Magento Core Controller (Checkout/OnepageController)
Are you struggling to override the core Checkout/OnepageController in your Magento store? You’re not alone! Overriding controllers can be tricky, especially for beginners.
Let’s dive into a step-by-step guide to help you successfully extend this core controller without touching any core files.
Why Avoid Direct Core File Modifications
Before we proceed, it’s crucial to understand why modifying core files is strongly discouraged:
- Updates and Compatibility: Changing core files can lead to compatibility issues when you update your Magento version.
- Performance Impact: Unnecessary modifications can slow down your store’s performance.
Step 1: Set Up Your Extension Structure
First, create a new directory structure for your extension under local -> Arias -> CoreExtended.
local
â
âââ Arias
âââ CoreExtended
âââ etc
â âââ config.xml
âââ controllers
â âââ Checkout
â âââ OnepageController.php
âââ app
âââ etc
âââ modules
âââ Arias_CoreExtended.xml
Step 2: Configure Your Extension’s Configuration File
Edit the config.xml file under local -> Arias -> CoreExtended -> etc.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Arias_CoreExtended>
<version>0.1.0</version>
</Arias_CoreExtended>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Arias_CoreExtended before="Mage_Checkout">Arias_CoreExtended_Checkout</Arias_CoreExtended>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Step 3: Configure Your Extension’s Module File
Edit the Arias_CoreExtended.xml file under local -> Arias -> CoreExtended -> app -> etc -> modules.
<?xml version="1.0"?>
<config>
<modules>
<Arias_CoreExtended>
<active>true</active>
<codepool>local</codepool>
</Arias_CoreExtended>
</modules>
</config>
Step 4: Create and Configure Your OnepageController
Finally, create the OnepageController.php file under local -> Arias -> CoreExtended -> controllers -> Checkout.
<?php
require_once 'Mage/Checkout/controllers/OnepageController.php';
class Arias_CoreExtended_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
public function collectAction()
{
echo 'WTF?';
}
public function indexAction()
{
echo "This controller has been overridden.";
}
}
Conclusion
Extending the core Checkout/OnepageController without modifying any core files is now possible. By following this structured approach, you can:
- Avoid potential compatibility issues during updates.
- Maintain a cleaner and more organized codebase.
- Easily identify custom changes when needed.
Remember to always backup your data before making any significant modifications to your Magento store. If you need further assistance or want to explore other aspects of Magento development, feel free to visit Laravel Company. We’re here to help!
Happy overriding!