In the end the best option seems to be creating an event listener for sales_quote_item_collection_products_after_load
to remove the products from the collection. This in turn causes the quote item to be marked for deletion, which in turn updates the database when the quote is saved. As far as I can tell this is how a quote item is removed when a product is disabled.
As per as checkout_cart_product_add_after
event.There are two parametera can be fetch from this event.
Your main issue with current quote items fetch.Need to use Event paramete for getting Quote Items
Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
One is current quote item
$CurrentQuoteObject=$Observer->getEvent()->getQuoteItem();
and current cart product
$CurrentProObject=$Observer->getEvent()->getProduct();
Get Current Quote object from Event params:
$QuoteObject=$CurrentQuoteObject->getQuote()
It is better idea to fetch Quote from event parameter which give the update quote.
code:
$items = $QuoteObject->getAllItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $productId) {
$itemId = $item->getItemId();
$QuoteObject->removeItem($itemId)->save();
break;
}
}
You can get quote item product by using $item->getProductType().$item field value take from sales_flat_quote_item table at Db
Best Answer
In the
Mage_Checkout_OnepageController::successAction
there is this line:where
This clears the cart completely.
But I wouldn't interfere with this process.
When you submit an order the quote associated to the order is inactivated and a link is made between the order and the quote.
I would leave this as it is and instead remember the products that you removed from the order and use this event
checkout_quote_destroy
(or an other one, but this is the first that came in mind), to recreate a new quote object for the current session where you add the products that were not included in the order.[EDIT]
or you can use this event
checkout_type_onepage_save_order_after
but I'm not 100% sure about it.