2

How to clear cart and minicart programatically in Magento 2?? I am using this code

$quoteId = $this->_checkoutSession->getQuote()->getId();
$quoteModel = $this->objectManager->create('Magento\Quote\Model\Quote');
$quoteItem = $quoteModel->load($quoteId);
$quoteItem->delete();

this clears cart but minicart items are still showing.. Can anyone please help me to fix this issue?? thanks

4

The mini cart items are from browser local storage. That means you need to reload the frontend cart section as well. An example here: https://magento.stackexchange.com/a/184745/33057

  • I have redirected to cart page after item delete.. but still it is not working.. – Keerthana Jan 25 '18 at 8:06
  • Make sure the cart section reloaded as well. – Khoa TruongDinh Jan 25 '18 at 8:16
  • You just want to empty cart ? Is it the case – Shashank Gupta Jan 25 '18 at 8:20
  • yes, I want to empty the cart as well as minicart – Keerthana Jan 25 '18 at 8:25
  • Thank you so much @Khoa TruongDinh.. I have reloaded cart section using your above link, and it is working.. – Keerthana Jan 25 '18 at 8:34
4

May be this code will help your logic :

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $cartObject = $objectManager->create('Magento\Checkout\Model\Cart')->truncate();
 $cartObject->saveQuote();

Remember that section is loaded after execution above code .

0

Currently on Magento 2.3.0 this code executed on backend will empty your cart and mini-cart:

$cart->truncate()->save();

Where $cart is an instance of \Magento\Checkout\Model\Cart.

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.