Loading...

How to clear shopping cart in Magento 2

Why would you want to delete items programmatically?

Sometimes you need to clear the cart when certain conditions. For example, when a customer adds a specific product, you want to remove all items from the cart. Or you may need to clear the cart when a customer applies a particular coupon code, Or you might need to remove a specific item from the cart.

There are several ways to clear the cart in Magento 2 programmatically.

We can discuss that in detail here.

1. Remove old items from the cart via the removeItem method and make it empty.

app/code/Elightwalk/Extension/Controller/EmptyCart/Index.php

php

<?php

namespace Elightwalk\Extension\Controller\EmptyCart;

use Magento\Framework\App\Action\Action;
 

class Index extends Action
{

   protected $modelCart;
   
   protected $checkoutSession;


   public function __construct(
       \Magento\Framework\App\Action\Context $context
       \Magento\Checkout\Model\Session $checkoutSession,
       \Magento\Checkout\Model\Cart $checkoutCart
   ){

       parent::__construct($context);
       $this->checkoutSession = $checkoutSession;
       $this->checkoutCart = $checkoutCart;

   }

 

   public function execute() {

       $cart = $this->checkoutCart;
       $items = $this->checkoutSession->getQuote()->getItemsCollection();
       foreach($items as $item) {
           $cart->removeItem($item->getId())->save();
       }
   }

}
?>

In this method, sometimes a cache issue comes once a redirect to the checkout page. For example, after clearing the cart programmatically, you must add new items and then redirect to Magento checkout. The total item count will have an issue.

2. delete items from the cart via the delete method of Quote Item Object.

app/code/Elightwalk/Extension/Controller/EmptyCart/Index.php

php

<?php

namespace Elightwalk\Extension\Controller\EmptyCart;

use Magento\Framework\App\Action\Action;

class Index extends Action
{

   protected $quoteItemFactory;

   protected $checkoutSession;
 

   public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Checkout\Model\Session $checkoutSession,
       \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory
   ) {

       parent::__construct($context);
       $this->checkoutSession = $checkoutSession;
       $this->quoteItemFactory = $quoteItemFactory;
   }
 

   public function execute() {

       $items = $this->checkoutSession->getQuote()->getItemsCollection();
       foreach($items as $item) {
           $_item = $this->quoteItemFactory->create()->load($item->getId());
           $_item->delete();
       }

   }

}

?>


 

In this method, you also face the same issue of cache, as explained above, when you redirect to the cart of checkout pages. If there is no need for a redirect and you only need to delete some items from the cart programmatically, then it will be good to go and use.

3. You can use the truncate method as a third option to delete items from the cart programmatically.

app/code/Elightwalk/Extension/Controller/EmptyCart/Index.php

php

<?php

namespace Elightwalk\Extension\Controller\EmptyCart;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
   protected $checkoutCart;

   public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Checkout\Model\Cart $checkoutCart
   ) {
       parent::__construct($context);
       $this->checkoutCart = $checkoutCart;
   }

   public function execute() {
       $this->checkoutCart->truncate();
   }

}
 

 

The above is recommended method to try while deleting all items from the cart programmatically, and you add new items and redirect to any pages. The cache issue will not come in the role here, and the output will be as expected; there is a con that it will remove all items from the cart.

The above method has the benefit that it will keep the same quote id.

As explained above, several methods exist to delete the items from the cart programmatically, so choose the way as per your requirement and archive the goal in minimal time.

Don't hesitate to contact us with more information regarding the Magento topic and notes.

 

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Blog
Duplicate entry error When getting an order using REST API in Magento

When retrieving orders using the Magento REST API, troubleshoot the 'Duplicate Entry' problem. Our guide gives insights and solutions to ensure a seamless and error-free order retrieval experience on Magento platform.

Read More
Blog
How to add customer last logged in column customer grid in Magento 2?

Easily improve client insights with Magento 2! Learn how to add a 'Last Logged In' column to the customer grid with ease. To optimize client management and personalized experience, follow our step-by-step tutorial.

Read More
Blog
How to create a module in Magento 2: Sample Module A Step-by-Step Guide

Embark on Magento 2 journey with confidence! Our step-by-step tutorial module development. Learn how to create a Module to harness the potential of Magento 2 customization for personalized and efficient e-commerce experience.

Blog Info