Automatically Update Cart when quantity changed in WooCommerce

Automatically-Update-Cart-when-quantity-changed-in-WooCommerce
5192 Views
3.9
(11145)

Today’s blog will allow you to have your cart totals automatically update when you modify the amount field without having to click the update button on the cart page.

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

Now that the button is hidden, all we need to do is “click” it with JQuery and let WooCommerce handle the rest.

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

I hope you found this post useful. If you like it, please share it. Please leave a remark if there is an issue. I’ll attempt to figure things out. CodexCoach.

Ready to experience breakthrough functionality? Your solution is one click away.

Unlock dynamic pricing on your WooCommerce store with Woo Product Quantity Range Price plugin. This plugin allows you to set different prices based on the quantity of products purchased, boosting bulk sales and customer satisfaction.

Perfect for wholesalers, retailers, and anyone looking to incentivize larger orders. Simplify your pricing strategy today!

Also Read:

How To Disable Payment Gateways For Specific User Roles in WooCommerce

How to Edit Payment Gateway Title and Description in WooCommerce

How to Create and apply Coupons Programmatically in WooCommerce

FAQ

In WordPress, how do I automatically update my cart?

To use this function, go to Appearance > Customize > WooCommerce > Cart and check the Auto Update box.

How can I add a quantity to my WooCommerce cart?

Navigate to WooCommerce > Settings > Advanced Product Quantity. then go to “Cart Quantities”

How useful was this blog?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 11145

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *