3

I am facing problem when i am removing configure product from cart using $cart->removeItem($itemId)->save(); method then its change all products price to zero. check this screen : https://prnt.sc/oagwbm

My code for removing item from cart :

foreach ($items as $item) {
    if($item->getProduct()->getId() == 208202){
        $this->_cart->removeItem($item->getItemId())->save();
    }
}

I am removing only one product from cart

Is there any other way to remove item from cart in observer ?

2
  • Please add your code here
    – Ravi Soni
    Commented Jul 4, 2019 at 11:22
  • @RaviSoni Please check my updated answer Commented Jul 4, 2019 at 11:36

1 Answer 1

3

You can try following code. It should be working fine I haven't tested it though.

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() == 'configurable') {
                $item->isDeleted(true);
            }
        }
    }
}
1
  • Thanks !! This $item->isDeleted(true); method solved my problem Commented Jul 4, 2019 at 14:15

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.