Magento:在观察者中停止添加到购物车的过程?

时间:2012-06-21 08:43:27

标签: magento-1.4 observers

我正在观察将产品添加到购物车时触发的事件。这是我用过的教程: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');
    }
}

1 个答案:

答案 0 :(得分: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...');
相关问题