Magento – How to refresh total sidebar when change qty by ajax in cart page

cartmagento2

I am changing the qty of items using ajax, when ajax is successful then i want to refresh my sidebar total.

Minicart updates automatically because of sections but the total does not update.

How can i do this?

Best Answer

I was doing the same, updating the item quantities in the Shopping Cart page using ajax, after my ajax call I do this:

storage.get(resourceUrlManager.getUrlForCartTotals(quote), false)
       .done(
           function (response) {
               quote.setTotals(response);
           })
       .fail(
           function (response) {
               //do your error handling
       });

In my JS file I imported these to make it works:

'Magento_Checkout/js/model/resource-url-manager','mage/storage' and 'Magento_Checkout/js/model/quote'

Also, storage can be replaced by an ajax request, with the next format:

$.ajax({
        url: resourceUrlManager.getUrlForCartTotals(quote),
        type: 'GET',
        global: false,
        contentType: 'application/json'
        });

but I wanted to keep the Magento style.

Related Topic