Mahi
Mahi

Reputation: 53

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

Upvotes: 2

Views: 6315

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

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.

Upvotes: 5

Related Questions