0

My client wants only one product of a particular type in the cart/quote. And it should be the most recent product added to the cart.

For example, user finds product. Adds to cart. User finds another product and adds to cart. I need to remove the first item and add the second.

I found this article which shows how to monitor items in the cart. I have it working, but I'm lost on the next step.

Ideas???

2 Answers 2

1

Have you tried deleting the existing items? So for example in the linked question just use this in the Observer class

if ($quote->getItemsCount() >= 1) { foreach ($quote->getItemsCollection() as $item) { $item->isDeleted(true); } } 

or

if ($quote->getItemsCount() >= 1) { foreach ($quote->getItemsCollection() as $item) { $quote->removeItem($item->getId()); } } 
0

You can also use below event/observer for the same:

<controller_action_predispatch_checkout_cart_index> <observers> <[MODULE NAME]> <class>[OBSERVER CLASS]</class> <method>checkoutRemoveProductFromCart</method>// method added in observer </[MODULE NAME]> </observers> </controller_action_predispatch_checkout_cart_index> 

Here in above code you need to change below constants added between [] brackets as per your custom module.

class [COMPANY NAME]_[MODULE NAME]_Model_Observer { public function checkoutRemoveProductFromCart(Varien_Event_Observer $observer) { $quote = Mage::getSingleton('checkout/session')->getQuote(); if ($quote ->getItemsCount() >= 1) { foreach ($quote->getItemsCollection() as $item) { $quote ->removeItem($item->getId()); } } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.