Skip to content

Instantly share code, notes, and snippets.

@no2pixel
Forked from mikaelz/functions.php
Created October 14, 2016 05:19
Show Gist options
  • Save no2pixel/5bd54e51ef2079c403aeef05b6312b00 to your computer and use it in GitHub Desktop.
Save no2pixel/5bd54e51ef2079c403aeef05b6312b00 to your computer and use it in GitHub Desktop.
WooCommerce auto-update cart when quantity changed
/**
* Auto update cart after quantity change
*
* @return string
**/
add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
if (is_cart()) :
?>
<script>
jQuery('div.woocommerce').on('change', '.qty', function(){
jQuery("[name='update_cart']").trigger("click");
});
</script>
<?php
endif;
}
@sambomcd
Copy link

Hi there. If it helps anyone, to solve the 'it works every second time' issue I had to...

Use a more specific selector:
jQuery('div.woocommerce').on('click', 'input.qty', function(){

remove the 'disabled' attribute from the Update Cart button before applying the click trigger:
jQuery("[name='update_cart']").removeAttr("disabled").trigger("click");

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