How Can I Empty Magento 2 Cart Programmatically

How can I empty Magento 2 cart programmatically

You will need to remove the Magento items one by one from Magento cart.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');
$allItems = $checkoutSession->getQuote()->getAllVisibleItems();
$cart =  $objectManager->get('Magento\Checkout\Model\Cart');

if(count($allItems) > 0){
    foreach ($allItems as $item) {
        $itemId = $item->getItemId();//item id of particular item
        $cart->removeItem($itemId)->save();
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments