3

I'm facing problem, I have added total 5 items in cart where 3 items are different colors of same configurable product and other 2 is simple products.

When i try to remove simple product from cart by observer "checkout_cart_product_add_after" using $cart->removeItem($itemId)->save(); method then its change all the configurable products price to zero(0) but the subtotal calculation is working fine.

Is there any other way to remove simple product from cart by observer ?

1

You can do something like that :

class DeleteObserver implements ObserverInterface
{
    protected $request;
    protected $cart;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Checkout\Model\CartFactory $cart
    ) {
        $this->cart = $cart;
        $this->request = $request;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $items = $this->cart->create()->getItems();
        foreach ($items as $item) {
            if ($item->getProductType() == 'simple') {
                $item->isDeleted(true);
            }
        }
    }
}

This is similar to this issue

  • @KKR : Did it work? – Rishabh Dwivedi Jul 5 at 4:40
  • it is working for me :-) Thanks for help! – KKR Jul 5 at 6:02
0

For remove product you can use 'sales_quote_remove_item' action for observer

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.