Magento 2: Triggering totals refresh in cart
In this tutorial, we will show how to trigger the totals block refresh in the cart.
What is the totals block?
The totals block is the block that displays totals - subtotal, discount, shipping rates, taxes and grand total -
How to cause the totals to refresh in the cart
Sometimes, when working on JavaScript code that changes totals, you might want the totals block to refresh. In order to do that, merge the following piece of code with your js code:
require(
[
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/cart/totals-processor/default'
],
function(
quote,
totalsDefaultProvider
) {
totalsDefaultProvider.estimateTotals(quote.shippingAddress());
}
);
Summing up
We just saw how to cause the totals block to refresh. If you have any questions about the code - please write me in comments.