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???

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

        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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