How to Programmatically Clear Shopping Cart in Magento 2

How to Clear Cart Programmatically m2

Since the era of e-commerce has started, developers are working hard to make CMS smarter and flexible with their coding capabilities. We developers are born to save time and efforts of end users by playing with codes and numbers. Also, day by day store owners become demanding to add new tons of functionality to improvise their store and stay ahead of the competition.

CHECK OUT: Magento 2 Save Cart Pro Extension for your store.

Recently while working on one of customers store, we came across the requirement of clearing shopping cart programmatically. Well, it is possible to clear the cart using three different ways which are mentioned below. Working with Magento several times you came across such a situation that’s why we thought of creating an article on Magecomp blog.

1. First method :
Create “Clearcart.php” file using following code at below location inside your custom extension folder.
app\code\vendor\extension\Controller\Index\Clearcart.php

namespace Vendor\Extension\Controller\Index;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Action\Action;
class Clearcart extends Action
{
	protected  $_modelCart;
	protected $ checkoutSession;
	public function __construct(CheckoutSession $checkoutSession,Cart $modelCart)
        {
        	$this->checkoutSession = $checkoutSession;
        	$this->_modelCart = $modelCart;
        }
	public function execute()
        {
		$cart = $this->_modelCart;
		$quoteItems = $this->checkoutSession->getQuote()->getItemsCollection();
		foreach($quoteItems as $item)
		{
			$cart->removeItem($item->getId())->save(); 
		}
	}
}

2. Second Method :
In this method, we can get the current cart using model cart object and then simply truncate it.

namespace Vendor\Extension\Controller\Index;
class Clearcart extends Action
{
	public function execute()
	{
		$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
		$cartObject = $objectManager->create('Magento\Checkout\Model\Cart')->truncate(); 
	 	$cartObject->saveQuote();
	}
}

3. Third Method :
In this last method, we can delete item from cart Modal using below code.

namespace Vendor\Extension\Controller\Index;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Model\Quote\Item
 
class Clearcart extends Action
{
	protected  $modelCartItem;
	protected $checkoutSession;

	public function __construct(CheckoutSession $checkoutSession,Item $modelCartItem)
        {
        	$this->checkoutSession = $checkoutSession;
                $this->_ modelCartItem = $modelCartItem;
        }

	public function execute()
	{
    		$checkoutSession = $this->getCheckoutSession();
		$allItems = $checkoutSession->getQuote()->getAllVisibleItems();
		foreach ($allItems as $item) 
		{
    			$cartItemId = $item->getItemId();
        		$itemObj=$this->getItemModel()->load($cartItemId);
       			$itemObj->delete();
		}
  	}
}

From give three methods, you can pick any one according to your need and choice.
Lastly, Comment down below if you face an issue while using this code.
Happy Cart Cleaning!

Previous Article

How to get Product List in System Configuration in Magento 2

Next Article

How to remove custom options price from the dropdown in Magento 2

Write a Comment
  1. Vignesvaran b

    Hi Dhiren,

    I have used your 3rd step cart cleared successfully. But in minicart shows the count as it before clearing the cart. Is any solution for this ?

    1. You need to implement the sections.xml into your custom module and implement the section update after your above method calls. So it will update the section after your code is executed.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨