Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Last active January 26, 2022 11:03
Show Gist options
  • Save mikaelz/f41e29c6a99a595602e4 to your computer and use it in GitHub Desktop.
Save mikaelz/f41e29c6a99a595602e4 to your computer and use it in GitHub Desktop.
WooCommerce auto-update cart when quantity changed, relates with https://gist.github.com/mikaelz/418a337dace188ba95b476ce65abfe0c
/**
* Auto update cart after quantity change
*
* @return string
**/
add_action( 'woocommerce_after_cart', 'custom_after_cart' );
function custom_after_cart() {
echo '<script>
jQuery(document).ready(function($) {
var upd_cart_btn = $(".update-cart-button");
upd_cart_btn.hide();
$(".cart-form").find(".qty").on("change", function(){
upd_cart_btn.trigger("click");
});
});
</script>';
}
@newbieboss
Copy link

@ross-bell thanks, your code works perfect and is better than the @gianlucaciralli code

Copy link

ghost commented Nov 20, 2017

add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
  if (is_cart()) :
   ?>
    <script>
    	jQuery(window).on('load', function(){
    		jQuery("[name='update_cart']").removeAttr('disabled');
    	});
    	jQuery( document.body ).on( 'updated_cart_totals', function(){
		    jQuery("[name='update_cart']").removeAttr('disabled');
		});
		jQuery('div.woocommerce').on('change', '.qty', function(){
           jQuery("[name='update_cart']").trigger("click"); 
        });
   </script>
<?php
endif;
}

@antoinehenrich
Copy link

This might be interesting, I had similar problems as you folks mentioned and I also had to handle some quantity buttons:
https://stackoverflow.com/questions/47392550/woocommerce-quantity-increment-with-buttons-not-working-after-ajax-refresh-and-a/4740007

@obajte01
Copy link

Hi guys, anyone have a solution for updating cart totals when is delivery method changed ?

@fabrizioschiavi
Copy link

@ghost solution works!

@pacmanito
Copy link

Any ideas how to prevent cart auto update if user tries to add stock for product with insufficient stock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment