admin管理员组

文章数量:1406926

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");
});

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");
});
Share Improve this question edited Aug 28, 2019 at 2:52 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Aug 26, 2019 at 19:40 MahiMahi 532 silver badges6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.woomerce-Price-amount').html();

        total = total.replace(/,/g, '.'); // Replace as 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.

本文标签: javascriptGet WooCommerce refreshed cart total on 39updatedcarttotals39 jQuery eventStack Overflow