2

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 ?

  • Please add your code here – Ravi Soni Jul 4 at 11:22
  • @RaviSoni Please check my updated answer – Mukesh Prajapati Jul 4 at 11:36
1

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);
            }
        }
    }
}
  • Thanks !! This $item->isDeleted(true); method solved my problem – Mukesh Prajapati Jul 4 at 14:15

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.