在Woocommerce中自动更新购物车项目数量更改中的购物车总数

时间:2019-05-22 13:03:31

标签: php jquery wordpress woocommerce cart

每当购物车中的产品数量自动增加或减少时,我都尝试更新购物车总数。我尝试了下面的代码,但是它始终无法正常工作,只有在刷新购物车页面后才一次;

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');

我希望每当产品数量发生变化时,购物车总数(将触发更新购物车按钮)。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要将其用作文档。主体委托事件(在changeinput事件上)如下:

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文件中。经过测试和工作。

类似:Avoid a function to only runs once on button click in WooCommerce cart

相关问题