-
-
Save no2pixel/5bd54e51ef2079c403aeef05b6312b00 to your computer and use it in GitHub Desktop.
| /** | |
| * 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; | |
| } |
works perfectly, thanks
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!
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; }
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.
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");
Hi, not working