To delete items from cart you need to follow these functions:
public function deleteQuoteItems(){ $checkoutSession = $this->getCheckoutSession(); $allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session foreach ($allItems as $item) { $itemId = $item->getItemId();//item id of particular item $quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id $quoteItem->delete();//deletes the item } } public function getCheckoutSession(){ $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager $checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');//checkout session return $checkoutSession; } public function getItemModel(){ $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager $itemModel = $objectManager->create('Magento\Quote\Model\Quote\Item');//Quote item model to load quote item return $itemModel; }
I hope this blog will help you with How to Delete Items from cart in Magento 2. You may also check our wide range of best Magento 2 Extensions.
Please reach out to our team via a support ticket if you have any queries.
Try this and if you have any queries then just comment below 🙂
I remove the item as your guide, however, the mini-cart is not updated. Can you please kindly advise what I can do? Thank you so much.