简体   繁体   中英

Get WooCommerce refreshed cart total on 'updated_cart_totals' jQuery event

I am using updated_cart_totals jQuery event to listen to WooCommerce cart updates. It works whenever I update my cart, but I need to get the new cart total as well.

Here is my basic code to listen 'updated_cart_totals' event:

$(document.body).on('updated_cart_totals', function (event) {
    alert("in here");
});

To get the refreshed cart total using updated_cart_totals event try the following:

jQuery( function($){
    $(document.body).on('updated_cart_totals', function () {
        // Get the formatted cart total
        var total = $('div.cart_totals tr.order-total span.woocommerce-Price-amount').html();

        total = total.replace(/,/g, '.'); // Replace comas by points
        total = parseFloat(total).toFixed(2); // Extract float number and format it with 2 decimals

        console.log(total); // Display it in the browser console
        alert(total); // Display it in an alert box
    });
});

It should work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM