简体   繁体   中英

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. 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). Tested and works.

Added also "input" event when customer input a value in the quantity field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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