【问题标题】:Get WooCommerce refreshed cart total on 'updated_cart_totals' jQuery event在“updated_cart_totals”jQuery 事件中获取 WooCommerce 刷新的购物车总数
【发布时间】:2019-12-31 00:06:33
【问题描述】:
我正在使用updated_cart_totals jQuery 事件来收听 WooCommerce 购物车更新。每当我更新我的购物车时它都会起作用,但我还需要获取新的购物车总数。
这是我监听“updated_cart_totals”事件的基本代码:
$(document.body).on('updated_cart_totals', function (event) {
alert("in here");
});
【问题讨论】:
标签:
javascript
jquery
wordpress
woocommerce
cart
【解决方案1】:
要使用updated_cart_totals 事件获取刷新后的购物车总数,请尝试以下操作:
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
});
});
它应该按预期工作。