0

我正在观察将产品添加到购物车时触发的事件。这是我使用的教程:http: //inchoo.net/ecommerce/magento/dispatching-before-and-after-events-to-magento-core-actions/

现在在某些情况下,我想停止将产品添加到购物车的过程。我尝试抛出异常,但这给了我处理您的请求错误消息时出现错误。检查由 Magento 创建的报告并没有告诉我任何信息。我还能如何停止添加过程?

这是我的代码:

public function hookToAddToCartBefore($observer) {
    ...
    if(somecondition) {
         Mage::throwException('some message');
    }
}
4

1 回答 1

1

特征:

  • 之后清除错误信息
  • 跳过所有“添加到购物车”代码
  • 之后重定向到您选择的页面

在尝试了所有可以想象的事情以尝试优雅地中止“添加到购物车”之后,我设法将各个部分组合起来,并通过单步调试源发现了标志 FLAG_NO_DISPATCH。其他更改参数或设置重定向的尝试将被“添加到购物车”过程中的某些代码覆盖。

    Mage::app()->getResponse()->setRedirect($store_product->getProductUrl());
    Mage::getSingleton('checkout/session')->addError(Mage::helper('checkout')->__('Sorry, the price of this product has been updated. Please, add to cart after reviewing the updated price.'));
    $observer->getControllerAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);  

您可以收到通知,而不是错误:

Mage::getSingleton('core/session')->addNotice('Sorry, this product is currently not available...');
于 2013-02-06T20:17:46.263 回答