我有一个观察者,如果商品缺货(即客户经常返回购物车 x 次,并且购物车中的商品缺货),它会从购物车中移除商品,并向用户显示一条消息。
删除商品有效,但更新购物车总数无效。 任何帮助将非常感激!
我的观察者观察到 sales_quote_save_before 事件:
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');
}