Reputation: 7993
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:
header()
", when Magento framework provides this functionality in an efficient way.Upvotes: 8
Views: 15322
Reputation: 181
You can use the _redirectUrl() method , it is used for redirecting to external websites from magento.
Upvotes: 1
Reputation: 724
I had to go through getFront
:
public function moduleMethod($observer) {
$observer->getEvent()->getFront()->getResponse()->setRedirect('http://www.google.com');
}
Upvotes: 2
Reputation: 23205
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 line
$this->_redirect('checkout/cart');
So 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!):
public function moduleMethod($observer) //note I added a param
{
/* @var $response1 Mage_Core_Controller_Response_Http */
$response1 = $observer->getResponse(); // observers have event args
$url = 'http://www.example.com/';
$response1->setRedirect($url);
/* SHOULDN'T DO THIS */
Mage::app()->getFrontController()->sendResponse();
}
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:
/**
* Set back redirect url to response
*
* @return Mage_Checkout_CartController
*/
protected function _goBack()
{
$returnUrl = $this->getRequest()->getParam('return_url');
if ($returnUrl) {
// clear layout messages in case of external url redirect
if ($this->_isUrlInternal($returnUrl)) {
$this->_getSession()->getMessages(true);
}
$this->getResponse()->setRedirect($returnUrl);
}
//...
}
It looks like we can just set a return_url
param on the request object and accomplish what we need.
public function moduleMethod(Varien_Event_Observer $observer)
{
$observer->getRequest()->setParam('return_url','http://www.google.com/');
}
I've tested this, and it should do the trick!
Upvotes: 17