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;
}
@lukasprelovsky
Copy link

Hi, not working

@tabasco86
Copy link

tabasco86 commented Apr 11, 2017

works perfectly, thanks

@Anikora
Copy link

Anikora commented May 1, 2017

Hi,
I was using your code in combination with a code to transform the quantity input to a dropdown (found it here: https://www.vanpattenmedia.com/2014/code-snippet-woocommerce-quantity-select-field) and the combination worked well until woocommerce version 3.0.

With woocommerce 3.0.X, it works only for every second update. So if I change the quantity, it does not update. If I change it again, it does update. If I change it a third time, it does nothing and if I change it a forth time, is does update. If I dont use the dropdown code, it updates every time. Any idea how to fix this? Many thanks!

@Anikora
Copy link

Anikora commented May 1, 2017

In addition to my previous post: I have no skills in javascript at all, but tried some code and the following worked on desktop and on mobile, but I am sure that this is really bad coded :)
Could you please give me a hint how to edit the code in order to get a proper solution? Many thanks in advance!

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

@Koendk
Copy link

Koendk commented May 19, 2017

Hey @Anikora, I've had the same problem. I've slightly changed the original poster his code:

add_action( 'wp_footer', 'cart_update_qty_script' ); function cart_update_qty_script() { if (is_cart()) : ?> <script> jQuery('div.woocommerce').on('click', '.quantity .button', function(){ jQuery("[name='update_cart']").trigger("click"); }); </script> <?php endif; }

Hope this fix your problem.

@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