raheem.unr
raheem.unr

Reputation: 877

How to clear/remove shopping cart items before adding new items in magento?

I tried with the following codes to remove/clear shopping cart items,it's removed all items in the shopping cart.but not adding the new product/item.

Used Code's

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();

please any one suggest me the above my requirements.Thanks in advance.

Thanks arumugam

Upvotes: 1

Views: 10219

Answers (3)

Raju Sutradhar
Raju Sutradhar

Reputation: 1

If you just want to update the quantity then check this it is solved my problem.

File Path : \app\code\core\Mage\Sales\Model\Quote File Name : Item.php

Line number : 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;
}

change to

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

Upvotes: 0

Abhishek Gupta
Abhishek Gupta

Reputation: 151

If you have a large number of customers quotes then deleting them by using loop might be time and resource consuming. You can clear/delete all customers cart items (all active sales quotes) by using the following SQL query:

DELETE FROM sales_flat_quote WHERE is_active = 1;

is_active = 0 means those quotes have been converted into orders, i.e. customer has placed order for those quotes.

is_active = 1 means quotes that have not been ordered, i.e. quotes present in the shopping cart of customers

Running this query will automatically delete related rows (quote items) from sales_flat_quote_item table through foreign key constraint

Upvotes: 2

raheem.unr
raheem.unr

Reputation: 877

here is the solution for remove as well as add products into cart

$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();

Upvotes: 5

Related Questions