In this event checkout_cart_add_product_complete
, I want the customer to be redirected to an external web page http://www.example.com/
. For this I am using this code, which is not working at all:-
public function moduleMethod() {
/* @var $response1 Mage_Core_Controller_Response_Http */
$response1 = $observer->getEvent()->getResponse();
/* @var $response2 Mage_Core_Controller_Response_Http */
$response2 = Mage::app()->getResponse();
$url = 'http://www.example.com/';
$response1->setRedirect($url);
return;
}
I have used the "setRedirect()
" method on both these variables "$response1
" and "$response2
", but both of them show me the Shopping Cart page, whereas I want to see this page http://www.example.com/
instead.
What I want:
- I don't want to override the Controller Class, just to redirect the Customer, when I can effectively use the Event Observer process.
- I don't want to use the PHP in-built function "
header()
", when Magento framework provides this functionality in an efficient way.
tl;dr: Correct observer code at the bottom.
A note before I answer: Make sure that the observer is being triggered; step through your code or use
die('here');
. As written, your example method does not have the correct prototype to receive event observer data (missing an argument).Using an event observer for redirect logic is totally appropriate in this context, as evinced by the core team explicitly passing the request and response objects in to the observer. Your attempt is good, but I think that you have conditions and configuration which cause execution to flow to
Mage_Checkout_CartController::_goBack()
, specifically to the lineSo we need to revise our approach. Now, you could prevent any request/response logic from processing after your event observer by manipulating the response and calling the Front Controller's
sendResponse()
method as demonstrated below (nb: don't do this!):This should work, but I think it mixes up areas of concern by triggering output from an ethereal system component (EDA). Let's see if there's something in the command-control structure which we can use...
Just after the
checkout_cart_add_product_complete
event execution comes to the cart controller's _goBack() method. The problem with this method name is that it does more than its name implies:It looks like we can just set a
return_url
param on the request object and accomplish what we need.I've tested this, and it should do the trick!