简体   繁体   English

自动触发更新 WooCommerce 购物车页面中的数量变化

[英]Auto trigger Update on quantity change in WooCommerce cart page

I removed the UPDATE button from woocommerce cart and I am trying to get the cart updated on quantity change on both desktop and mobile.我从 woocommerce 购物车中删除了 UPDATE 按钮,我试图在桌面和移动设备上更新购物车的数量变化。 The following script makes the job but for some reason it only works once.以下脚本完成了这项工作,但由于某种原因它只能工作一次。 Hope someone can help, thanks so much!希望有人能帮忙,非常感谢!

add_action( 'wp_footer', 'update_cart_qty' ); 
function update_cart_qty() {
   if (is_cart()) {
      ?>
      <script type="text/javascript">
         jQuery('input.qty').change(function(){
            jQuery("[name='update_cart']").trigger("click");
         });
      </script>
      <?php
   }
}

To make it work, you need to delegate the "change" event to the document body, this way:要使其工作,您需要将“更改”事件委托给文档正文,如下所示:

add_action( 'wp_footer', 'auto_update_cart_on_qty_change' );
function auto_update_cart_on_qty_change() {
    if ( is_cart() ) :
    ?>
    <script type="text/javascript">
    (function($){
        $( document.body ).on( 'change input', 'input.qty', function() {
            $('[name=update_cart]').trigger('click');
        });
    })(jQuery);
    </script>
    <?php
    endif;
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

Added also "input" event when customer input a value in the quantity field.当客户在数量字段中输入一个值时,还添加了“输入”事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM