Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

lukasprelovsky commented Mar 10, 2017

Hi, not working

@tabasco86

This comment has been minimized.

Copy link

tabasco86 commented Apr 11, 2017

works perfectly, thanks

@Anikora

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

sambomcd commented Aug 24, 2017

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
You can’t perform that action at this time.