Auto update cart after quantity change

I am trying to Auto update the woocommerce cart after quantity is changed. The following code within the function.php is working, BUT updates the cart only if I change the quantity twice. Do you know how to fix that?

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;
}

3 Answers
3

Almost one year late, but this question might still get visitors:
You trigger the click, but the button doesn’t have enough time to become enabled, so that is why, by the time you click the second time the button becomes enabled. Remove the “disabled” propriety before triggering the click:

    <script>
    jQuery('div.woocommerce').on('change', '.qty', function(){
        jQuery("[name="update_cart"]").prop("disabled", false);
        jQuery("[name="update_cart"]").trigger("click"); 
    });
    </script>

Leave a Comment