如何在magento中添加新项目之前清除/删除购物车项目?

时间:2013-08-13 07:34:52

标签: magento

我尝试使用以下代码删除/清除购物车商品,它已删除购物车中的所有商品。但未添加新商品/商品。

二手代码

1.Mage::getSingleton('checkout/session')->clear();
2.Mage::getSingleton('checkout/cart')->truncate();
3.$quote = Mage::getSingleton('checkout/session')->getQuote(); 
$quote->delete();
4.Mage::getSingleton('checkout/cart')->truncate()->save();
5.Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
6.foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}
7.$cartHelper = Mage::helper('checkout/cart');
        $items = $cartHelper->getCart()->getItems();       
        foreach ($items as $item)
        {
                $itemId = $item->getItemId();
                $cartHelper->getCart()->removeItem($itemId)->save();
        }

8.$quote = Mage::getSingleton('checkout/session')->getQuote();
$item = $quote->getItemByProduct($product);
$quote->removeItem($item->getId());

9.$quote->removeAllItems();

请任何人建议我以上我的要求。谢谢。

由于 的Arumugam

3 个答案:

答案 0 :(得分:5)

这里是删除以及将产品添加到购物车中的解决方案

$params['qty'] = 1;
$product = $this->_initProduct($_product); // here initialize the product

$cart = Mage::getModel('checkout/cart');                
$cart->truncate(); // remove all active items in cart page
$cart->init();
$cart->addProduct($product,$params);                        
$cart->save();

答案 1 :(得分:1)

如果您有大量客户报价,则使用循环删除它们可能会耗费时间和资源。您可以使用以下SQL查询清除/删除所有客户购物车项目(所有活动销售报价):

DELETE FROM sales_flat_quote WHERE is_active = 1;

is_active = 0表示这些报价已转换为订单,即客户已下订单。

is_active = 1表示尚未订购的报价,即客户购物车中的报价

运行此查询将通过外键约束从sales_flat_quote_item表中自动删除相关行(引用项)

答案 2 :(得分:0)

如果您只是想更新数量,请检查一下它是否解决了我的问题。

文件路径:\ app \ code \ core \ Mage \ Sales \ Model \ Quote 文件名:Item.php

行号:285

public function addQty($ qty)     {         $ oldQty = $ this-> getQty();         $ qty = $ this-> _prepareQty($ qty);

    /**
     * We can't modify quontity of existing items which have parent
     * This qty declared just once duering add process and is not editable
     */
    if (!$this->getParentItem() || !$this->getId()) {
        $this->setQtyToAdd($qty);
        $this->setQty($oldQty + $qty);

    }
    return $this;
}

更改为

public function addQty($ qty)     {         $ oldQty = $ this-> getQty();         $ qty = $ this-> _prepareQty($ qty);

    /**
     * We can't modify quontity of existing items which have parent
     * This qty declared just once duering add process and is not editable
     */
    if (!$this->getParentItem() || !$this->getId()) {
        $this->setQtyToAdd($qty);
        //$this->setQty($oldQty + $qty);
        $this->setQty($qty);
    }
    return $this;
}
相关问题