0
votes

I have a hook in the checkout process of magento, in the observer: checkout_controller_onepage_save_shipping_method. This observer action happens once a user chooses a shipping method in Magento then clicks the "next" button to save the shipping method and continue through the checkout process.

I have written code that may sometimes tell the user (depending on various factors such as shopping cart items and the state they are being shipped to) that he may not proceed with the checkout process and must call the store instead for special shipping instructions.

The regular redirect functions don't work inside onepage checkout because it is wrapped in an AJAX script, I have tried the following:

Mage::getSingleton('core/session')->addError('The shipping required for one of the products you are attempting to order..');

$response1 = $observer->getResponse(); 

$url = Mage::getUrl('checkout/cart');

$response1->setRedirect($url);

and

Mage::throwException($this->__('The shipping required for one of the products you are attempting to order requires special instructions, please call us.')); 

$observer->getRequest()->setParam('return_url','http://www.google.com/');
            exit;

and

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
Mage::app()->getResponse()->sendResponse();
exit;

But none of these work. I believe I have to override some functions in OnePageController.php but I do not know what I need to update. Any help would be greatly appreciated. Thanks!

2

2 Answers

1
votes

I had the same problem recently when I was trying to create a error message on onepage checkout process and after a several tries I could cancel the process and alert the user with an error message (my goal was to do this in a Observer function).

My code:

$result['success'] = false;
$result['error'] = true;
$result['error_messages'] = implode(', ', $errors);
$response = Mage::app()->getResponse();
$response->setBody(Mage::helper('core')->jsonEncode($result));
$response->sendResponse(); 
exit;
0
votes

You're setting the redirect on and attaching the error to the AJAX request. Because the steps are controlled by javascript, you have two options:

  • Display the error and redirect to the cart when the customer submits his or her order rather than after the shipping step.
  • Redirect in javascript, either by redefining window.location or submitting a hidden form

The first would be easier, but the second would provide a better user experience. You would need to redirect to a custom controller in order to add an error message and send the user back to the cart page.