Automatically update cart when changing quantity (Woocommerce

I an trying to automatically update my Cart when a client clicks on the plus or minus button to cahneg the quantity of a product in his/her cart.
Therefore, I found the following code on the internet:

// First, hide the Update Cart button
add_action( 'wp_head', 'ecommercehints_hide_update_cart_button' );
function ecommercehints_hide_update_cart_button() { ?>
	<style>
		button[name="update_cart"], input[name="update_cart"] {
			display: none;
		}
</style>
<?php }


// Second, add the jQuery to update the cart automaitcally on quantity change
add_action( 'wp_footer', 'ecommercehints_update_cart_on_quantity_change');
function ecommercehints_update_cart_on_quantity_change() { ?>
	<script>
	jQuery( function( $ ) {
		let timeout;
		$('.woocommerce').on('change', 'input.qty', function(){
			if ( timeout !== undefined ) {
				clearTimeout( timeout );
			}
			timeout = setTimeout(function() {
				$("[name='update_cart']").trigger("click");
			}, 500 ); // 500 being MS (half a second)
		});
	} );
	</script>
<?php }

This seems to work in a template but not on the actual Cart page
Could someone have a look at it?

There you go, add the code to your functions.php child theme:

function actualizar_carrito_automaticamente(){
    ?>
    <script type="text/javascript">
        (function($){
            $( document.body ).on( 'change input', 'input.qty', function(){
                $("[name='update_cart']").trigger("click");
            });
        })(jQuery);
    </script>
    <?php
}
add_action( 'wp_footer', 'actualizar_carrito_automaticamente' );



Enjoy

4 Likes

Is this still not in bricks by default?

1 Like