Woocommerce 中购物车商品数量变化时自动更新购物车总数
Auto update cart totals on cart item quantity change in Woocommerce
每次购物车中的产品数量自动增加或减少时,我都会尝试更新购物车总数。我尝试了下面的代码,但它并不总是有效,仅在刷新购物车页面后有效一次;
function update_cart_refresh_update_qty() {
if (is_cart()) :
?>
<script type="text/javascript">
jQuery( function($){
$('.qty').on('change', function(){
setTimeout(function() {//This is set, so it gives the update cart button time to enable from disable mode
$('input[name="update_cart"]').trigger('click');
}, 2000);
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'update_cart_refresh_update_qty');
我希望每次产品数量发生变化时更新购物车总数(触发更新购物车按钮)。
提前感谢您的帮助。
您需要将其用作 document.body 委托事件 (在 change
和 input
事件上),如下所示:
add_action( 'wp_footer', 'update_cart_on_item_qty_change');
function update_cart_on_item_qty_change() {
if (is_cart()) :
?>
<script type="text/javascript">
jQuery( function($){
$(document.body).on('change input', '.qty', function(){
$('button[name="update_cart"]').trigger('click');
// console.log('Cart quantity changed…');
});
});
</script>
<?php
endif;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
相似:
每次购物车中的产品数量自动增加或减少时,我都会尝试更新购物车总数。我尝试了下面的代码,但它并不总是有效,仅在刷新购物车页面后有效一次;
function update_cart_refresh_update_qty() {
if (is_cart()) :
?>
<script type="text/javascript">
jQuery( function($){
$('.qty').on('change', function(){
setTimeout(function() {//This is set, so it gives the update cart button time to enable from disable mode
$('input[name="update_cart"]').trigger('click');
}, 2000);
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'update_cart_refresh_update_qty');
我希望每次产品数量发生变化时更新购物车总数(触发更新购物车按钮)。
提前感谢您的帮助。
您需要将其用作 document.body 委托事件 (在 change
和 input
事件上),如下所示:
add_action( 'wp_footer', 'update_cart_on_item_qty_change');
function update_cart_on_item_qty_change() {
if (is_cart()) :
?>
<script type="text/javascript">
jQuery( function($){
$(document.body).on('change input', '.qty', function(){
$('button[name="update_cart"]').trigger('click');
// console.log('Cart quantity changed…');
});
});
</script>
<?php
endif;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
相似: