Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    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.

    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.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close