Automatically Update Cart when quantity changed in WooCommerce

Automatically Update Cart when quantity changed in WooCommerce
45 Views

Remove the WooCommerce Update Cart Button

<?php
add_action( 'wp_head', 'cxc_cart_refresh_update_hide_button' );
function cxc_cart_refresh_update_hide_button() { 
	?>
	<style>
		.woocommerce button[name="update_cart"],
		.woocommerce input[name="update_cart"] { display: none; }
	</style>
	<?php
}
?>

Automatically WooCommerce Cart when Quantity Changes

<?php
add_action( 'wp_footer', 'cxc_cart_refresh_update_qty' ); 
function cxc_cart_refresh_update_qty() { 
	if ( is_cart() || ( is_cart() && is_checkout() ) ) {
		?>
		<script>
			jQuery( function( $ ) {
				let timeout;
				jQuery('.woocommerce').on('change', 'input.qty', function(){
					if ( timeout !== undefined ) {
						clearTimeout( timeout );
					}
					timeout = setTimeout(function() {
						jQuery("[name='update_cart']").trigger("click"); // trigger cart update
					}, 1000 ); // 1 second delay, half a second (500) seems comfortable too
				});
			} );
		</script>
		<?php
	}
}
?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.