0

I have customized mini-cart. and place one new button Clear shopping cart. it's work fine. all item are deleted successfully. but mini cart total not updated in Magento 2. total updated only by one item.

check snap. https://i.stack.imgur.com/pUvWY.png

2

To update the mini-cart in you need to create a sections.xml in your module etc/frontend directory.

<?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="checkout/cart/add">
        <section name="cart"/>
    </action>
</config>

Just replace checkout/cart/add with your modulename/controller/actionname and clear your webshop cache. You can also find the same thing in magento-checkout module from below path,

app\code\Magento\Checkout\etc\frontend\sections.xml

Magento does this job from below path (Just for debugging purpose),

Magento\Customer\view\frontend\web\js\customer-data.js

$(document).on('ajaxComplete', function (event, xhr, settings) {
    var sections,
        redirects;

    if (settings.type.match(/post|put|delete/i)) {
        sections = sectionConfig.getAffectedSections(settings.url);

        if (sections) {
            customerData.invalidate(sections);
            redirects = ['redirect', 'backUrl'];

            if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line
                return;
            }
            customerData.reload(sections, true);
        }
    }
});

Alternatively, you can also do by adding just piece of javascript code on your AJAX (I am assuming you are doing this operation by AJAX)

require([
           'Magento_Customer/js/customer-data'
        ], function (customerData) {
            var sections = ['cart'];
            customerData.invalidate(sections);
            customerData.reload(sections, true);
        });
8
  • nice answer bro...
    – Amit Bera
    Nov 22 '17 at 12:49
  • i have try this but it update only for 1 item
    – Ajay Patel
    Nov 22 '17 at 13:45
  • @keyur Shah can you please help me in this question, magento.stackexchange.com/questions/279346/…
    – Mujahidh
    Jun 24 '19 at 10:06
  • what is this section.xml used for,
    – Mujahidh
    Jun 24 '19 at 10:12
  • @AjayPatel did you get the solution. i am facing same issue
    – Arjun
    Jun 29 '20 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.