Session Magento在observer中重新计算购物车总数

Session Magento在observer中重新计算购物车总数,session,magento,observer-pattern,cart,Session,Magento,Observer Pattern,Cart,我有一个观察者,如果商品缺货(即,客户在x时间返回他们的购物车,购物车中的商品缺货),它会从购物车中移除商品,并向用户显示一条消息 删除项目有效,但更新购物车总数无效。如有任何帮助,将不胜感激 我的观察者观察活动前的销售报价保存情况: public function checkStockStatus($observer) { // return if disabled or observer already executed on this request if (!Mage::

我有一个观察者,如果商品缺货(即,客户在x时间返回他们的购物车,购物车中的商品缺货),它会从购物车中移除商品,并向用户显示一条消息

删除项目有效,但更新购物车总数无效。如有任何帮助,将不胜感激

我的观察者观察活动前的销售报价保存情况:

public function checkStockStatus($observer)
{
    // return if disabled or observer already executed on this request
    if (!Mage::helper('stockcheck')->isEnabled() || Mage::registry('stockcheck_observer_executed')) {
        return $this;
    }

    $quote = $observer->getEvent()->getQuote();
    $outOfStockCount = 0;

    foreach ($quote->getAllItems() as $item) {
        $product = Mage::getModel('catalog/product')->load($item->getProductId());
        $stockItem = $product->getStockItem();
        if ($stockItem->getIsInStock()) {
            // in stock - for testing only
            $this->_getSession()->addSuccess(Mage::helper('stockcheck')->__('in stock'));
            $item->setData('calculation_price', null);
            $item->setData('original_price', null);
        }
        else {
            //remove item 
            $this->_getCart()->removeItem($item->getId());
            $outOfStockCount++; 
            $this->_getSession()->addError(Mage::helper('stockcheck')->__('Out of Stock'));
        }
    }

    if ($outOfStockCount) > 0) {       
        $quote->setTotalsCollectedFlag(false)->collectTotals();
    } 

    Mage::register('stockcheck_observer_executed', true);

    return $this;         
}

protected function _getCart()
{
    return Mage::getSingleton('checkout/cart');
}

protected function _getSession()
{
    return Mage::getSingleton('checkout/session');
}  
当天提示:通过观察*\u在之后保存并尝试强制更改同一对象 通常会再次调用保存,您将进入无休止的循环.oO

但是,如果您观察quote类中的collectTotals()方法,您会注意到缺少一个重要标志
->setTotalsCollectedFlag(false)->collectTotals()
,以便在计算完成后进行计算

如果在通往荣耀的道路上没有bug,生活会有所不同,因此请注意Magento中的以下问题:

下一个流程如何:

  • sales\u quote\u save\u before
    上删除观察者中的项目,并在注册表中添加一些标志:
    Mage::register('ooops\u我们需要重定向',$url)

  • 如果需要,请在
    sales\u quote\u save\u poster上的observer中进行重定向:

    if(Mage::registry('ooops\u我们需要\u重定向')){ //重定向 }


  • 谢谢@Anton的帮助

    最终对我有效的答案是打电话到
    session\u write\u close()重定向前(在观察者中):


    谢谢你今天的提示!编辑代码以删除无限循环(之前观察*\u save\u,不重新调用
    save()
    )。但是,即使在调用
    ->collectTotals()
    之前添加
    ->setTotalsCollectedFlag(false)
    ,总计也不会更新。我还遗漏了什么?重新加载后您将看到更改?是的,但消息消失。如果我在我的观察者中进行重定向,我会立即得到更新的总数,但也不会收到任何消息。我想要的是两者:)试着给save打个电话,看看结果会是什么。你太棒了,Anton,我从过去13个小时就开始打这个循环了$quote->setTotalsCollectedFlag(false)->collectTotals();我看不出这将如何解决会话消息(即
    Mage::getSingleton('chekout/session')->addError('error message here');
    )在重定向/页面刷新时丢失的问题?我看不到“在observer中重新计算购物车总数”与“会话消息(即
    Mage::getSingleton('chekout/session')->addError)”之间的关系('error message here');
    )在重定向/页面刷新时丢失?…您完全正确,我的问题的标题没有它本来应该具有的描述性。更糟糕的是,我编辑了问题,无意中删除了额外的澄清(为了简洁)。挑战在于能够重新计算购物车总数并向用户显示会话消息。我已经能够实现删除项目和重新计算总数(通过调用
    collectTotals()
    )后观察者中的重定向)或删除项目并显示消息(无重定向)你可以接受答案,然后让其他人知道答案已经解决了
    if (// products are out-of-stock and were removed...) {
        $this->_getSession()->addError('Error message here.');
        $this->_getSession()->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
        session_write_close();
        Mage::app()->getResponse()->setRedirect('index');
    }