2

Is there any way that we can remove the Items from the cart. Actually I have dynamic Grouped products were I need to allow the user to buy the item inside the grouped product. Now when someone only select the Item under the grouped product then it allow to buy that and need to stop or remove the group product from the cart.

I had tried with checkout_cart_product_add_after Observer and used below logic, but it is not working

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

Please help me guys.

Thanks in advance.

1 Answer 1

2

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

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.