I tried two different approaches for this, but both failing at different point.

  1. By calling $cart->truncate()->save() and then $cart->addProduct($product, $qty). But instead of replacing the whole cart with new items, overlapping items (with or without the same quantity) will be deleted anyways.
  2. I also tried $quote->removeAllItems()->collectTotals()->save() and then $cart->addProduct($product, $qty), but when the quantity of an overlapping item is collectively larger than stock level, it will prompt a false out of stock error message.

More on 2

I have dived into the code which checks stock level, which is Mage_CatalogInventory_Model_Observer::checkQuoteItemQty().

Every time I test with code in 2), a total of four times it checked and in the 3rd time the $qtyForCheck of that particular overlapping item is summed up with quantities before AND after removeAllItems(), effectively generating a false error prompt.

  • 1) isn't working for you as you need to call it as follows: Mage::getSingleton('checkout/cart')->truncate();
    – Moose
    Commented Jan 5, 2015 at 6:35
  • @moose but $cart->truncate() is actually a wrapper to $quote->removeAllItems()
    – Vicary
    Commented Jan 5, 2015 at 6:47

1 Answer 1


Why not re-declare cart & start over like this?

$session = Mage::getSingleton('checkout/session');

$cart = Mage::getModel('checkout/cart');

$product = Mage::getModel('catalog/product') -> load($product_id);
if ($product) {
    if ($product -> isSaleable()) {
        $cart -> addProduct($product, array('qty' => $qty));

// $session->setCartWasUpdated(true); // Not really necessary - Vicary
  • AFAIK the Cart model is just a wrapper to the Quote model, the Quote id is stored in checkout/session and this should end up working on the same quote. One still need to clear quote items before calling $cart->addProduct().
    – Vicary
    Commented Jan 5, 2015 at 6:31
  • I've updated the answer to include the delete call on the current quote model. P.S. The reason 1) may not be working for you is because you're not calling the ->setCartWasUpdated(true); function.
    – Moose
    Commented Jan 5, 2015 at 6:33
  • Tried to delete the quote with no luck. The latter created cart still loads the same quote which is going to be deleted.
    – Vicary
    Commented Jan 5, 2015 at 6:50
  • Found the solution, after quote deletion one must call Mage::getSingleton('checkout/session')->clear() before the cart can generate a new quote. Please add that to your answer and I will accept it. :)
    – Vicary
    Commented Jan 5, 2015 at 6:58
  • Ah, good find. Have approved your edit.
    – Moose
    Commented Jan 5, 2015 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.