Prevent Quantity Change in the WooCommerce Cart

By default, your customers can change the quantity of a product whilst on the WooCommerce cart. In most situations, that’s great, but there are circumstances where this is less than ideal.

Using the snippets below, you can prevent your customers from being able to change the quantity on the cart page. The snippets work by replacing the quantity field with a static number displaying the quantity instead.

The first snippet applies to all products on your store, whilst the second snippet only applies to specific products. The last snippet allows you to target all products except specific ones. You’ll only need to use the snippet that’s relevant to your use case.

Apply to all products

This snippet will disable the WooCommerce cart quantity field for all products:

add_filter( 'woocommerce_cart_item_quantity', 'snippetpress_disable_wc_cart_quantity', 10, 3 );
function snippetpress_disable_wc_cart_quantity( $product_quantity, $cart_item_key, $cart_item ) {
	
	$quantity_num = isset( $cart_item['quantity'] ) ? $cart_item['quantity'] : 1;

	return (string) $quantity_num;
	
}

Apply to specific products

This snippet will disable the WooCommerce cart quantity for specific products. Just add your product IDs in the $disable_qty array on line 4:

add_filter( 'woocommerce_cart_item_quantity', 'snippetpress_disable_wc_cart_quantity_specific_products', 10, 3 );
function snippetpress_disable_wc_cart_quantity_specific_products( $product_quantity, $cart_item_key, $cart_item ) {
	
	$disable_qty = array(123, 456, 789); // Add your WC product IDs to the array
	
	$product_id   = $cart_item['product_id'];
	$quantity_num = isset( $cart_item['quantity'] ) ? $cart_item['quantity'] : 1;

	if ( in_array( $product_id, $disable_qty ) ) {
		return (string) $quantity_num;
	}

	return $product_quantity;
	
}

Apply to all products except specific products

This snippet will allow you to disable the WooCommerce cart quantity field on all products except for the ones you specify. Just add the products that you want to exclude from the snippet to the $enable_qty array on line 4:

add_filter( 'woocommerce_cart_item_quantity', 'snippetpress_disable_wc_cart_quantity_exclude_specific_products', 10, 3 );
function snippetpress_disable_wc_cart_quantity_exclude_specific_products( $product_quantity, $cart_item_key, $cart_item ) {
	
	$enable_qty = array(123, 456, 789); // Add your WC product IDs to the array
	
	$product_id   = $cart_item['product_id'];
	$quantity_num = isset( $cart_item['quantity'] ) ? $cart_item['quantity'] : 1;

	if ( !in_array( $product_id, $enable_qty ) ) {
		return (string) $quantity_num;
	}

	return $product_quantity;
	
}

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.