防止 Observer 被调用两次

Prevent Observer to be called twice

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

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

P.S。我正在使用 Magento 1.9.2

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

$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
    }