3

During the checkout process I sometimes want to programmatically remove items from the session's quote. So I tried this code:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$all_quote_items = $quote->getAllItems();
foreach ($all_quote_items as $item) {
    $quote->removeItem($item->getId())->save();
}

However, after this loop the list of items in the $quote object is still the same, i.e. no items have been removed.

Any ideas what I am missing here?

Using Magento 1.4.1.1

7ochem
  • 2,183
  • 1
  • 34
  • 42
Matthias
  • 9,817
  • 14
  • 66
  • 125

4 Answers4

14

Try

$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();        
foreach ($items as $item) 
{
   $itemId = $item->getItemId();
   $cartHelper->getCart()->removeItem($itemId)->save();
} 

See http://www.magentocommerce.com/boards/viewthread/30113/

MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • Thanks, that helped me to spot the problem. – Matthias Apr 17 '13 at 16:10
  • 4
    Isn't this saving the cart on each iteration? Why would you do that? Remove the items in the iteration and save the cart outside of it. – nevvermind Apr 22 '14 at 13:27
  • @Renon how to use $items = $cartHelper->getCart()->getItems(); this line in API ? Can I get Cart by cart ID or customer ID using above codes ? – Sachin Vairagi Jan 05 '17 at 14:31
  • @SachinVairagi ... can you provide more info or sample code of what you are trying to do – MagePal Extensions Jan 05 '17 at 14:50
  • require_once '../app/Mage.php'; Mage::app('default'); $proxy = new SoapClient('http://www.yxyshareoflove.stag-innov8te.com/api/soap/?wsdl'); $sessionId = $proxy->login('apitest', 'kavya@123'); $cartId = $_REQUEST['quote_id']; $cartHelper = Mage::helper('checkout/cart'); $items = $cartHelper->getCart($cartId)->getItems(); if(count($items->getData())) { foreach ($items as $item) { $itemId = $item->getItemId(); $cartHelper->getCart()->removeItem($itemId)->save(); } echo json_encode(array('status' => true , 'message' => '.')); } – Sachin Vairagi Jan 05 '17 at 14:55
  • Thanks much for your reply , please check my codes in above comment – Sachin Vairagi Jan 05 '17 at 14:56
  • @SachinVairagi ... you will need to use the magenot api since your code is hosted outside of magento... see http://devdocs.magento.com/guides/m1x/api/soap/checkout/cartProduct/cartProduct.html – MagePal Extensions Jan 05 '17 at 17:52
  • @RenonStewart - My products have custom option so I need to remove it using Item ID , can you please let me know if there is any way to do it ? It will be really helpful , thanks much. – Sachin Vairagi Jan 06 '17 at 04:41
  • @RenonStewart, shall we remove only specific product from cart and save it? if (9 == $item->getProductId()) { $this->cart->removeItem($itemId); $quote = $this->cart->getQuote(); $quote->collectTotals()->save(); } – Jafar Pinjar Mar 13 '19 at 13:37
7

In Magento 1.7.0.0 version, you can use:

Mage::getSingleton('checkout/cart')->truncate()->save();
Reena Parekh
  • 933
  • 1
  • 8
  • 24
Sertekmedia
  • 171
  • 1
  • 2
0

I do a similar process while looking for items of a certain type, The logic I applied is:

$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();

$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach ($cartItems as $item)
{
    $quote->removeItem($item->getId())->save();
}

Try the above and if that fails I would start dumping the quote objects out before and after this logic is executed to see what differences there are.

James Cowie
  • 464
  • 3
  • 5
  • 1
    No, I copied your code 1:1 but the items are still in the $quote object after the loop. – Matthias Apr 17 '13 at 12:54
  • ok, In the loop try adding: Zend_Debug::dump($quote->removeItem($item->getId())->save());die(); and let me know what this returns, It should either be NULL or some helpful data. – James Cowie Apr 17 '13 at 13:08
  • I used your code but it removed from a database table. But I want to remove from quote session after quote create – Pratik Mehta Aug 21 '18 at 04:05
0

Try the below code It will work

$product = $observer->getEvent()->getProduct();
$cart = Mage::getSingleton('checkout/cart');
foreach ($cart->getQuote()->getItemsCollection() as $_item) {
    if ($_item->getProductId() == $productId) {
            $_item->isDeleted(true);
    //Mage::getSingleton('core/session')->addNotice('This product cannot be added to shopping cart.');
        }
    }