WooCommerce: Conditionally Force Product Quantity 1 @ Cart

There are times when the WooCommerce product settings alone are not enough. You can already tick the “Sold individually” checkbox in the “Inventory” product data tab in the single product edit page to force quantity 1 for whatever product: “Enable this to only allow one of this item to be bought in a single order“.

Problem is, you may need to set this “programmatically” (via code), based on certain conditions. One reason is that you may not want to edit hundreds of products one by one (or in bulk) – another is that you may want to “override” whatever settings based on certain conditions (for example, you set “Sold Individually”, but if the Cart total is greater than 100 you want to allow quantities greater than 1).

As you can see, in this post we will cover, once again, the magic of “conditional logic“. Enjoy!

Yes, you can force max add to cart quantity to 1 via the product settings. But code is nicer and can be made dynamic or conditional.

PHP Snippet 1: Force “Sold Individually” If Product Stock Is Low

/**
 * @snippet       Conditionally Force Sold Individually @ WooCommerce Cart
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer, BusinessBloomer.com
 * @testedwith    WooCommerce 4.5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_is_sold_individually', 'bbloomer_product_max_1_cart_stock_low', 9999, 2 );

function bbloomer_product_max_1_cart_stock_low( $individually, $product ) {
	if ( $product->get_stock_quantity() < 3 ) {
		$individually = true;
	}
	return $individually;
}

PHP Snippet 2: Force “Sold Individually” If Product ACF Field Exists

/**
 * @snippet       Conditionally Force Sold Individually @ WooCommerce Cart
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer, BusinessBloomer.com
 * @testedwith    WooCommerce 4.5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_is_sold_individually', 'bbloomer_product_max_1_cart_custom_field', 9999, 2 );

function bbloomer_product_max_1_cart_custom_field( $individually, $product ) {
	$acf_field_value = get_field( 'acf_field_id', $product->get_id() );
	if ( $acf_field_value && 'whatever' == $acf_field_value ) {
		$individually = true;
	}
	return $individually;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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