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