I have the extension CreativeStyle CheckoutByAmazon installed on my magento 1.7 store and I am trying to override the CheckoutController Class but magento seems to ignore my override. Any suggestions will be much appreciated. Also cleared cache but still doesnt work
(In app/code/local Folder)
MyModule \CheckoutByAmazon \controllers \CheckoutController.php \etc \config.xml
(app/etc/config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyModule_CheckoutByAmazon>
<version>0.1.0</version>
</MyModule_CheckoutByAmazon>
</modules>
<frontend>
<routers>
<checkoutbyamazon>
<args>
<modules>
<MyModule_CheckoutByAmazon before="Creativestyle_CheckoutByAmazon">MyModule_CheckoutByAmazon</MyModule_CheckoutByAmazon>
</modules>
</args>
</checkoutbyamazon>
</routers>
</frontend>
</config>
(In app/code/local/MyModule/CheckoutByAmazon/controllers/CheckoutController)
<?php
//Controllers are not autoloaded so we will have to do it manually:
require_once 'Creativestyle/CheckoutByAmazon/controllers/CheckoutController.php';
class MyModule_CheckoutByAmazon_CheckoutController extends Creativestyle_CheckoutByAmazon_CheckoutController {
public function indexAction() {
exit('I am here');
parent::indexAction();
}
//i want to override this function
public function saveShippingAction() {
//insert my overriding code
exit('saveShipping');
}
}
?>
(In etc/modules/MyModule_CheckoutByAmazon.xml)
<?xml version="1.0"?>
<config>
<modules>
<MyModule_CheckoutByAmazon>
<active>true</active>
<codePool>local</codePool>
</MyModule_CheckoutByAmazon>
</modules>
</config>
Where am I going wrong?
tl;dr: The original extension config is likely the problem, but here's how to troubleshoot.
In order to begin debugging it's necessary to understand what you are trying to do. Without any custom modules in present, Magento will take the request path frontname "checkout", match it to the directory ./app/code/core/Mage/Checkout/controllers/ and then look for a match of the second and third parameters as a combination of file, class definition in that file, and action method in that class. Non-deprecated rewrite syntax (which you are trying to perform with your module) simply adds an additional directory for matching. Before debugging this, it's best to cover the basics. This presents a good opportunity to debug controller overrides.
There are a number of preconditions which must be satisfied (your module's config.xml getting merged, correct syntax & file structure, etc.). It's good to test that these are working first, as it will reduce and pinpoint the areas where we need to debug.
In a test.php file in your Magento root, do the following:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL^E_STRICT);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();
$origDir = Mage::getModuleDir('controllers','Creativestyle_CheckoutByAmazon').DS;
require_once $origDir.'CheckoutController.php';
$controller = new MyModule_CheckoutByAmazon_CheckoutController(
Mage::app()->getRequest(), //required constructor arg
Mage::app()->getResponse() //required constructor arg
);
echo get_class($controller);
Visit yoursite.com/test.php and you should see your controller class name. If you see an error, or you see nothing, something fairly low-level is broken. If the error message is unintuitive, check that your module config is being merged and move from there.
If the class instantiates, we know the following:
- Base module config is good
- File structure is appropriate for the config
- The explicit include is good
My suspicion is that you will make it this far, because I believe the issue to be config-based. There are a few possibilities. To gain insight, amend test.php to the following (assuming PHP >= 5.3):
<?php
ini_set('display_errors',1);
error_reporting(E_ALL^E_STRICT);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();
$router = Mage::app()->getFrontController()->getRouter('standard');
/* @var $router Mage_Core_Controller_Varien_Router_Standard */
$reflection = new ReflectionClass($router);
$modules = $reflection->getProperty('_modules');
$modules->setAccessible(true);
var_dump($modules->getValue($router));
The output should resemble something like the following:
array(35) {
["core"]=>
array(1) {
[0]=>
string(9) "Mage_Core"
}
["install"]=>
array(1) {
[0]=>
string(12) "Mage_Install"
}
["directory"]=>
array(1) {
[0]=>
string(14) "Mage_Directory"
}
// ...
["checkout"]=>
array(2) {
[0]=>
string(25) "MyModule_CheckoutByAmazon"
[1]=>
string(13) "Mage_Checkout"
}
// ...
}
Wherever you see more than one subarray, there is a controller directory rewrite.
To complete C4rter's answer, here is the full code you have to add into your config.xml file :
<admin>
<routers>
<checkoutbyamazon>
<args>
<modules>
<MyModule_CheckoutByAmazon before="Creativestyle_CheckoutByAmazon_Adminhtml">MyModule_CheckoutByAmazon</MyModule_CheckoutByAmazon>
</modules>
</args>
</checkoutbyamazon>
</routers>
</admin>
You don't need the frontend rewrite part.
Tried to do the same and failed also. But I found the reason. The CheckoutByAmazon Extension has an admincontroller of the same name:
<admin>
<routers>
<checkoutbyamazon>
<use>admin</use>
<args>
<module>Creativestyle_CheckoutByAmazon</module>
<frontName>checkoutbyamazon</frontName>
</args>
</checkoutbyamazon>
</routers>
</admin>
So you have to rewrite the admin controller:
before="Creativestyle_CheckoutByAmazon_Adminhtml"
来源:https://stackoverflow.com/questions/15814708/overriding-a-magento-controller-in-community-extension