Read More
Read More
Menu Close

    Update cart totals by js on checkout page magento2

    If you are working on checkout page functionality, and you are updating the totals by js. After this total is not reflecting/updating on summery block, then to update amount by JS after your request without reloading your page, you can use following process:

    you need to add following code in your js file:

    /*browser:true*/
    /*global define*/
    define(
        [
            'ko',
            'jquery',
            'Magento_Checkout/js/model/cart/totals-processor/default',
            'Magento_Checkout/js/model/cart/cache'
        ],
        function (ko, $, defaultTotal, cartCache) {
            'use strict';
            return Component.extend({
                updateamount:function () {//your function to update amount
                    //your code to update amount
                    //after successfull execution you need to add these lines.
                    cartCache.set('totals',null);
                    defaultTotal.estimateTotals();
                }
            });
        }
    );

    Here, firstly you need to define js

    'Magento_Checkout/js/model/cart/totals-processor/default',
    'Magento_Checkout/js/model/cart/cache'

    after this create objects for the same, i used here defaultTotal and cartCache.

    Now, add these lines in your file after which you want to update the totals.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    cartCache.set('totals',null);
    defaultTotal.estimateTotals();

    cartCache.set(‘totals’, null) : set the cart total to calculate totals again, if not using this line then estimateTotals() returns the totals for cache.

    defaultTotal.estimateTotals(): executing the process to calculate totals.

    Order total in default process:

    after executing code it will be like:

    Hope this will help you in manage totals of cart.

     

    Thank you.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    1 comments

  • Andres
    Hi in the past, I am using Magento 2 2.3 and it does not work, could you put the source if it is not too much trouble?
  • Back to Top