How to Update Total Using JS File in Magento 2
While working with the cart page and checkout page, the total automatically updates when you enter a correct coupon code or discount code in the default behavior of Magento 2.
What if you want to apply that same behavior in your custom discount method, store credit, or rewards? You can update total using JS file in Magento 2, the same as in the default Magento 2.
It’s bad practice to reload the whole page to update the total on modifying any field, and it results in a bad user experience too!
The users need not wait while the page reloads to proceed to place the order. Hence, use the below solution to automatically update total using JS file in Magento 2 cart page and checkout page.
Method to Update Total Using JS File in Magento 2
Use the below code in .js file.
1 2 3 4 5 6 7 8 9 |
define([ 'jquery', 'Magento_Checkout/js/action/get-totals' ], function ($, getTotalsAction) { 'use strict'; var deferred = $.Deferred(); getTotalsAction([], deferred); }); |
That’s it
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Solved: Navigation / Mega Menu is Missing When Varnish Cache is Activated in Magento 2
How to Move Related Products Above Product Tabs in Magento 2
Next