【问题标题】:Auto update cart totals on cart item quantity change in Woocommerce在 Woocommerce 中自动更新购物车商品数量变化的购物车总数
【发布时间】:2019-10-08 23:31:50
【问题描述】:

每当购物车中的产品数量自动增加或减少时,我都会尝试更新购物车总数。我尝试了下面的代码,但它并非一直有效,只有在购物车页面刷新后才有效;

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

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

提前感谢您的帮助。

【问题讨论】:

    标签: php jquery wordpress woocommerce cart


    【解决方案1】:

    您需要将其用作 document.body 委托事件(在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多