【问题标题】:Prevent Observer to be called twice防止观察者被调用两次
【发布时间】:2017-09-18 08:53:46
【问题描述】:

我有一个在 catalog_controller_product_view 事件上触发的观察者。它工作正常,但是当我将产品添加到购物车时,Magento 会产生另一个事件,然后重定向回产品视图,在这里我的观察者再次被调用。

我想阻止这种行为。我试图在第一个产品视图和从购物车重定向后记录整个 $observer 对象,但它们绝对相同。 任何想法,如何防止 Observer 在此事件中被调用两次?

附:我正在使用 Magento 1.9.2

【问题讨论】:

    标签: magento magento-1.9


    【解决方案1】:

    我不确定我的解决方法有多好,但它确实有效。

    $coreSession = Mage::getSingleton('core/session');
    if ($event_name == 'catalog_controller_product_view' && $coreSession->getData("test") == 1){
            $coreSession->setData("test", 0);
            return;
        }
    // view
    if ($event_name == 'catalog_controller_product_view'){
            $coreSession->setData("test", 0);
            // Do stuff
        }
    // add_to_cart 
    elseif ($event_name == 'checkout_cart_add_product_complete'){
            $coreSession->setData("test", 1);
            // Do stuff
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2021-04-13
      • 2015-11-05
      相关资源
      最近更新 更多