Get subtotal, shipping total and Grand total in checkout page using js Magento 2.

You can get total related information in the checkout page with a subtotal, shipping charge, grand total using Javascript.

For getting update grand total with the shipping charge and tax total and other info you need to call ‘Magento_Checkout/js/model/totals’ in the JS file.

You need to call totals Objects with a required field,

define([
    'uiComponent',
    'Magento_Checkout/js/model/totals'
], function (Component,totals) {
    'use strict';

    return Component.extend({
        getGrandTotal: function(){
            if (totals.totals()) {
                var grandTotal = parseFloat(totals.totals()['grand_total']);
                return grandTotal;
            }
        },
        getSubTotal: function(){
            if (totals.totals()) {
                var subtotal = parseFloat(totals.totals()['subtotal']);
                return subtotal;
            }
        },
        getShippingAmount: function(){
            if (totals.totals()) {
                var shippingAmount = parseFloat(totals.totals()['shipping_amount']);
                return shippingAmount;
            }
        }
    });
});

You can call above function in template with getGrandTotal(), getShippingAmount() and getSubTotal().

4 Replies to “Get subtotal, shipping total and Grand total in checkout page using js Magento 2.”

  1. How can i set grand total in cart page and same total should be followed to the check out page.
    $cartQuote = $this->cart->getQuote();
    error_log($cartQuote->getGrandTotal());

    The above code will give me the cart total

    And to set the grand total
    $grandTotal = "200";
    $cartQuote->setGrandTotal($grandTotal);
    $this->quoteRepository->save($cartQuote);

    the above code is not changing the grandtotal

    Please help me to set the grand total

      1. Each Customers have few points in their account. 1 point = 1 Rupee.
        I want to give a textbox and a button in a cart page when customer enters the points in the text box i want to subtract that points with the grand total and update the grandtotal

Leave a Reply