Magento 2 – Fix Mini Cart Not Updating When Deleting Product

magento2mini-cart

I see my mini cart not updating its items when I delete an item from the cart programmatically.

but the cart updating perfectly as per my requirement only after I added a new item into cart or any cart event-triggered non programmatically.

Best Answer

You can fix this issue by creating a file app\code\Vendor\Extension\etc\frontend\section.xml using following code inside your extension folder.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="extension/controller/action">
        <section name="cart"/>
    </action>
</config>

In this action parameter name, you have to pass the full action (For ex. checkout/cart/add ) in which cart is updated. It means you programmatically delete or add a product to cart to the shopping cart.

Also, you can add below code in your observer to refresh the quote for the customer.

$quote = $this->quoteRepository->get($quoteId);
$quote->setCustomerId($customer->getId()); // Whatever you want to update
$this->quoteRepository->save($quote);

Reference: how to refresh mini cart programmatically in magento2

Hope it helps!!!

Related Topic