I try to overwrite the function indexAction from "app\code\core\Mage\Newsletter\controllers\ManageController.php" with my own.
I duplicated the controller to app\code\local\Fekete\Newsletter2Go\controllers\ManageController.php and only let the function indexAction there.
<?php
require_once 'Mage/Newsletter/controllers/ManageController.php';
class Fekete_Newsletter2Go_ManageController extends Mage_Newsletter_ManageController
{
public function indexAction()
{
exit("test");
}
}
Then I added the following code inside the <config></config> tags in: app\code\local\Fekete\Newsletter2Go\etc\config.xml:
<frontend>
<routers>
<newsletter>
<args>
<modules>
<Fekete_Newsletter2Go before="Mage_Newsletter">
Fekete_Newsletter2Go
</Fekete_Newsletter2Go>
</modules>
</args>
</newsletter>
</routers>
</frontend>
But If I go to http://example.com/newsletter/manage/ then nothing has changed, my overwrite was not used.
What am I missing?
I found the cause for the problem. I had to change this:
<Fekete_Newsletter2Go before="Mage_Newsletter">
Fekete_Newsletter2Go
</Fekete_Newsletter2Go>
to this:
<Fekete_Newsletter2Go before="Mage_Newsletter">Fekete_Newsletter2Go</Fekete_Newsletter2Go>
Related
Attempting to override the Cms/controllers/IndexController.
This is the directory structure of my module -
Local/MyCompany/FourOhFourExp/controllers/IndexController.php
Local/MyCompany/FourOhFourExp/etc/config.xml
The code for my IndexController is
<?php
require_once 'Mage'. DS .'Cms'. DS .'controllers'. DS .'IndexController.php';
class MyCompany_FourOhFourExp_IndexController extends Mage_Cms_IndexController
{
public function noRouteAction($coreRoute = null){
Mage::log('First Function');
header("Location: new_route.html");
die();
}
public function defaultNoRouteAction()
{
Mage::log('Second function!');
header("Location: new_route.html");
die();
}
}
?>
and this is the code for my config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_FourOhFourExp>
<version>0.1.0</version>
</MyCompany_FourOhFourExp>
</modules>
<frontend>
<routers>
<cms>
<args>
<modules>
<MyCompany_FourOhFourExp before="Mage_Cms">MyCompany_FourOhFourExp</MyCompany_FourOhFourExp>
</modules>
</args>
</cms>
</routers>
<events>
<controller_action_noroute>
<observers>
<cms>
<class>cms/observer</class>
<method>noRoute</method>
</cms>
</observers>
</controller_action_noroute>
</frontend>
</config>
Hoping someone can explain why this isn't overriding. I've looked through numerous tutorials and blog posts but have yet to figure out why it doesn't work.
There are lot of issue:
1)Modulepath:
As your controller class is MyCompany_FourOhFourExp_IndexController then path of class should be
app/code/local/MyCompany/FourOhFourExp
Local/MyCompany/controllers/IndexController
Local/MyCompany/etc/config.xml
change to
Local/MyCompany/FourOhFourExp/controllers/IndexController/php
Local/MyCompany/FourOhFourExp/etc/config.xml
2)Mage cms controller donot add properly
require_on Mage::getModuleDir('controllers', 'Mage_Cms'). DS .'IndexController.php';
What need to do:
add to app/code/local/Mage/Cms/controllers/IndexController.php , Mage::dispatchEvent('cms_controller_action_noroute', array('action'=>$this));
in your custom module, catch the Event
I want to bypass the magento cart by clicking on a normal save button which submits the form via ajax and then redirects to onepage checkout by bypassing cart (like using windows.location.href)....please help
Sonam,
You can create an observer for this purpose as follows :
Your config would look something like this:
<frontend>
<events>
<checkout_cart_add_product_complete>
<observers>
<packagename_modulename_observer>
<type>singleton</type>
<class>packagename_modulename/observer</class>
<method>addToCartComplete</method>
</packagename_modulename_observer>
</observers>
</checkout_cart_add_product_complete>
</events>
</frontend>
You could create an observer listening for the checkout_cart_add_product_complete event and in there you could do something like the following:
public function addToCartComplete(Varien_Event_Observer $observer) {
// Send the user to the Item added page
$response = $observer->getResponse();
$request = $observer->getRequest();
$response->setRedirect(Mage::getUrl('checkout/onepage'));
Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
}
Second and fairer simpler would be :
Go to app/code/core/Mage/Checkout/controllers/ in CartController.php
Copy this File and search method
protected function _goBack()
change
$this->_redirect('checkout/cart');
to
$this->_redirect('checkout/onepage');
hope these methods help you complete the process as you want.
I have been trying to do the same. I recommend SKVs second solution (editing the CartController) but of course you shouldn't edit the core. And overriding a controller is not as simple as, say, overriding a model object.
While you can copy for example app\code\core\Mage\Checkout\Model\Cart.php to app\code\local\Mage\Checkout\Model\Cart.php, make your edits and just be done with it, overriding a controller class is not that simple. To the best of my knowledge, you have to do it in your own module. Here's what I did.
Made a module: app\code\local\Companyname\Checkout.
Added a etc\config.xml to this module:
<config>
<modules>
<Companyname_Checkout>
<version>0.1.0</version>
</Companyname_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Companyname_Checkout before="Mage_Checkout">Companyname_Checkout</Companyname_Checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Added a controllers\CartController.php to my module. This is a copy of the core CartController but I changed
class Mage_Checkout_CartController extends Mage_Core_Controller_Front_Action
to:
require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'CartController.php');
class Pixelkracht_Checkout_CartController extends Mage_Checkout_CartController
Finally, make the changes in the _goBack function as suggested by SKV.
I would like to do the above.
Ive overridden many files in the past...block, model, helper....but this one eludes me.
Can anyone see what im doing wrong here:
(ive edited this code...to include some of the recomendations now...)
Heres my folder structure (2 controller locations as a test):
/Idigital/Idgeneral/etc/config.xml
/Idigital/Idgeneral/controllers/Checkout/CartController.php
/Idigital/Idgeneral/controllers/CartController.php
Heres my config.xml:
<?xml version="1.0"?>
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
<blocks>
<idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
</blocks>
</global>
<frontend>
<routers>
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
<layout>
<updates>
<idgeneral>
<file>idigital.xml</file>
</idgeneral>
</updates>
</layout>
</frontend>
</config>
Ihave placed my controller file in 2 locations to test.
And heres the top of my FIRST controller file:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside checkout/cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
Ans my second controller:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
When i visit: /checkout/cart/add
Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).
When i visit /checkout/cart/test - i get a 404
When i visit /cart/add or cart/test - i get a 404
when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404
Create your module folders and files
app/code/local/MyNameSpace/MyModule/etc/config.xml
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
app/etc/modules/MyNameSpace_All.xml
Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:
<?xml version="1.0"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<global>
<!-- This rewrite rule could be added to the database instead -->
<rewrite>
<!-- This is an identifier for your rewrite that should be unique -->
<!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<!--
- mymodule matches the router frontname below
- checkout_cart matches the path to your controller
Considering the router below, "/mymodule/checkout_cart/" will be
"translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
-->
<to>/mymodule/checkout_cart/</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
<!--
If you want to overload an admin controller this tag should be <admin> instead,
or <adminhtml> if youre overloading such stuff (?)
-->
<frontend>
<routers>
<mynamespace_mymodule>
<!-- should be set to "admin" when overloading admin stuff (?) -->
<use>standard</use>
<args>
<module>MyNameSpace_MyModule</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>mymodule</frontName>
</args>
</mynamespace_mymodule>
</routers>
</frontend>
Note: The above didn’t work for me when I override catalog/product controller. I had to use:
<from><![CDATA[#^catalog/product/#]]></from>
<to>mymodule/mycontroller</to>
(notice the missing leading slash)
Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Please note that before=”Mage_Checkout” will load your controller first if available and fall back to Magento’s if not.
If the controller is in another folder, you’ll have to modify the code:
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.
Replace
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
with
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
Edit 'controllers/Checkout/CartController.php'
Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):
<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends
Mage_Checkout_CartController
{
# Overloaded indexAction
public function indexAction() {
# Just to make sure
error_log('Yes, I did it!');
parent::indexAction();
}
}
Edit 'app/etc/modules/MyNameSpace_All.xml'
(This is to activate your module)
true
local
Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:
<mynamespace_mymodule_checkout_cart_index>
<update handle="checkout_cart_index"/>
</mynamespace_mymodule_checkout_cart_index>
(Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)
[by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]
The above item did not work for me (2009-02-19 by Jonathan M Carvalho)
I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml
Add the following lines:
Point your browser to /checkout/cart/
In your PHP error log, you should find ‘Yes, I did it!’.
You need to be extra precise with the rewrite regular expression because errors are very hard to find.
<Idigital_Idgeneral before="Mage_Checkout">Idgeneral_Checkout</Idigital_Idgeneral>
Should be
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
or try moving your custom controller up to
../Idigital/Idgeneral/controllers/CartController.php
and use
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral</Idigital_Idgeneral>
There is also an error in your <modules> tag location. It should be:
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
...
</global>
<frontend>
....
</frontend>
...
</config>
i.e <modules> shouldn't be inside <global>
Here's a good tutorial on how to dump the config tree that Magento sees as XML: http://alanstorm.com/magento_config
I'm leaving this here for the next poor developer forced to work with this jalopy. Much of the instructions here are pasted from the magento docs which like its source, is a twisted labyrinth of misdirection. Okay enough complaints...
This worked for me in version 1.8
Create your namespace and module:
/app/code/local/MyNameSpace/MyModule
Create your module config:
/app/code/local/MyNameSpace/MyModule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Create your controller:
/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction() {
// /var/log/debug.log should log the output
Mage::log('cart index override', null, 'debug.log');
// Call the parent class
parent::indexAction();
}
}
Enable your new module:
/app/etc/modules/MyNameSpace_All.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyNameSpace_MyModule>
</modules>
</config>
That's all thats needed. Now go celebrate, you just polished a turd! ;)
This is a little notification on the include path of the controller.
This include path can cause errors if the Magento Compiler mode is turned on.
require_once 'Mage/Checkout/controllers/CartController.php';
Instead of that it is good to use
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
It will be safer.
Well...it WONT override the checkout cart controller.
So i used the URL REWRITE in the PRODUCT VIEW...from this link...near the bottom of the article. They say its a proper method...
http://www.excellencemagentoblog.com/magento-add-product-to-cart-ajax
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","idgeneral/cart");
That worked for me. Allows me to get cracking. Basically calls MY controller..instead of checkout controller.
Thanks for the help ROSCIUS...appreciated.
I also had to change my config....my routers section now looks like this:
<routers>
<!-- THIS PART REGISTERS OUR CONTROLLERS FOLDER FOR USE -->
<idgeneral>
<use>standard</use>
<args>
<module>Idigital_Idgeneral</module>
<frontName>idgeneral</frontName>
</args>
</idgeneral>
<!-- THIS PART WONT WORK TO OVERWRITE OUR MAGE CONTROLLER -->
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
I'm building a Controller and when I call an Action I get a 404 Error (everything worked last week!). I'm calling the action using:
http://www.website.com/route/controllername/actionname
eg:
http://www.website.com/abcdef/test/echo
I have investigated the problem (using Mage::log('..')) and found out that the controller is being loaded. The controller's name is fine too. Further investigation revealed that the noRouteAction within my controller is being called.
ie my controller contains:
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
When I call http://www.website.com/abcdef/test/echo I get "noRoute Action".
Why isn't echoAction being called?
Edit:
The config.xml
<?xml version="1.0"?>
<config>
<modules>
<Abcdef_Web>
<version>1.0.0</version>
</Abcdef_Web>
</modules>
<global>
<helpers>
<abcdef>
<class>Abcdef_Web_Helper</class>
</abcdef>
</helpers>
<blocks>
<abcdef>
<class>Abcdef_Web_Block</class>
</abcdef>
</blocks>
</global>
<frontend>
<routers>
<abcdef>
<use>standard</use>
<args>
<module>Abcdef_Web</module>
<frontName>abcdef</frontName>
</args>
</abcdef>
</routers>
<layout>
<updates>
<abcdef>
<file>abcdef.xml</file>
</abcdef>
</updates>
</layout>
</frontend>
</config>
The controller class file: (TestController.php)
<?php
class Abcdef_Web_TestController extends Mage_Core_Controller_Front_Action {
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
}
?>
OK found the problem!!!!
It seems that "Add Store Code to Urls" was set to yes, so Magento was expecting:
http://www.website.com/storecode/abcdef/test/echo
rather then:
http://www.website.com/abcdef/test/echo
For those with the same problem. This can be disabled through:
System / Configuration / General / Web / Url Options / Add Store Code to Urls
Note:
I do not remember setting this option to Yes. I've seen some reports that this setting could be changing automatically (4 people reporting it):
http://www.magentocommerce.com/boards/viewthread/192613/
I am following this tutorial
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics.
As per this tutorial,when i pass a value with url like(http://localhost/dev/weblog/index/testModel/id/1), I am getting error like this
Fatal error: Call to a member function load() on a non-object in app\code\local\Srivats\Weblog\controllers\IndexController.php on line 10
This is my index controller code
<?php
class Srivats_Weblog_IndexController extends Mage_Core_Controller_Front_Action
{
public function testModelAction()
{
$params = $this->getRequest()->getParams();
$blogpost = Mage::getModel('weblog/blogpost');
echo("Loading the blogpost with an ID of ".$params['id']);
$blogpost->load($params['id']);
$data = $blogpost->getData();
var_dump($data);
}
}
Blogpost.php file
<?php
class Srivats_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost','blogpost_id');
}
}
Here is my config file
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<modules>
<srivats_weblog>
<version>1.0.0</version>
</srivats_weblog>
</modules>
<models>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog_mysql4>
</models>
</global>
<frontend>
<routers>
<weblog>
<use>standard</use>
<args>
<module>Srivats_Weblog</module>
<frontName>weblog</frontName>
</args>
</weblog>
</routers>
</frontend>
</config>
system.log shows
Warning: include() [function.include]:
Failed opening 'Mage\Weblog\Model\Blogpost.php' for inclusion
(include_path='app\code\local;E:\dev\app\code\community;app\code\core;E:\dev\lib;.;C:\php\pear')
in lib\Varien\Autoload.php on line 93
Here is the link to all files https://gist.github.com/cf99e4277599954f38d4
I think this line $blogpost->load($params['id']); causing trouble.What i am missing.Any pointers?
Ahh in you config you should have
<global>
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
</model>
</global>
you already have for mysql4 resource add this on top of that. You should have
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
</weblog_mysql4>
</models>
Your real problem is that the line:
$blogpost = Mage::getModel('weblog/blogpost');
Is not returning an object and so $blogpost is just NULL.
Is the blogpost model class file actually created in app/code/local/Srivats/Weblog/Model/Blogpost.php