简体   繁体   English

magento单页结帐,创建错误消息并重定向

[英]magento onepage checkout, creating an error message and redirect

I have a hook in the checkout process of magento, in the observer: checkout_controller_onepage_save_shipping_method. 我在观察者的magento的结帐过程中有一个钩子: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. 一旦用户在Magento中选择一种送货方式,然后单击“下一步”按钮以保存该送货方式并继续执行结帐过程,就会发生此观察者操作。

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: 常规重定向功能在一页结帐中不起作用,因为它包装在AJAX脚本中,我尝试了以下操作:

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. 我相信我必须重写OnePageController.php中的某些功能,但我不知道需要更新什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

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). 最近,当我尝试在一页结帐过程中创建错误消息时遇到了同样的问题,经过几次尝试后,我可以取消该过程并用错误消息提醒用户(我的目标是在Observer函数中执行此操作)。

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;

You're setting the redirect on and attaching the error to the AJAX request. 您正在设置重定向并将错误附加到AJAX请求。 Because the steps are controlled by javascript, you have two options: 由于步骤是由javascript控制的,因此您有两个选择:

  • 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 通过重新定义window.location或提交隐藏的表单在javascript中进行重定向

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. 您需要重定向到自定义控制器,以添加错误消息并将用户发送回购物车页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM