Sometimes you only want customers to buy one thing.
If you sell digital licenses, event tickets, or one-off bookings, you might need to stop customers from mixing that product with anything else in their cart. Other times, you simply want to prevent them from buying more than one unit of a limited item.
WooCommerce only supports the second scenario out of the box. To handle both properly, you’ll combine a small built-in option with a short snippet that enforces the single-product rule at checkout.
Case 1 — Limit to One Product Per Order
Place the snippet below in a small functionality plugin or your child theme’s functions.php. It blocks carts from mixing a “solo” product with anything else. It also prevents adding a solo product to an already non-empty cart and prevents adding other products when a solo product already sits in the cart.
Before you make any code changes, have a backup of your site. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.
/**
* @snippet Limit Purchases to One Per Order
* @author Nicola Mustone
* @author_url https://nicolamustone.blog/2018/06/17/limit-purchases-one-per-order/
* @tested-up-to WooCommerce 10.3.X
* @license GPLv2
*/
add_filter( 'woocommerce_add_to_cart_validation', 'nm_limit_one_product_per_order', 10, 5 );
function nm_limit_one_product_per_order( $passed, $product_id, $quantity, $variation_id = 0, $variations = array() ) {
// IDs of products that must be purchased alone.
$solo_products = array( 31 ); // ← replace with your product IDs
// Safety checks for admin/non-cart contexts.
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return $passed;
}
if ( ! function_exists( 'WC' ) || ! WC()->cart ) {
return $passed;
}
$cart = WC()->cart;
$adding_is_solo = in_array( (int) $product_id, $solo_products, true );
// 1) If adding a solo product and the cart already has anything, block.
if ( $adding_is_solo && $cart->get_cart_contents_count() > 0 ) {
wc_add_notice( __( 'This product must be purchased alone. Please complete or empty your current cart first.' ), 'error' );
return false;
}
// 2) If the cart already contains a solo product, block adding anything else.
foreach ( $cart->get_cart() as $item ) {
$in_cart_product_id = isset( $item['product_id'] ) ? (int) $item['product_id'] : 0;
if ( in_array( $in_cart_product_id, $solo_products, true ) ) {
// If the same solo product is being added again, also block to avoid mixing with quantity changes.
wc_add_notice( __( 'This product must be purchased alone. Please complete or empty your current cart first.' ), 'error' );
return false;
}
}
return $passed;
}
This validation runs on every “Add to cart” action, including AJAX adds and quantity changes from product pages. For true “only one item overall” behavior for a solo product, keep its Sold individually box unchecked; the snippet enforces solitude at the cart level, not quantity.
If you need to limit multiple products, add more product IDs on this line:
$solo_products = array( 31, 32, 33 );
Case 2 — Limit to One Item Per Product
WooCommerce supports this natively:
- Go to Products → All Products.
- Edit the product.
- In Inventory, check Sold individually.
Customers can still buy other products in the same order, but they won’t be able to set the quantity above one for that product.

Which approach should you use?
- Use “one product per order” (code) when the product has special fulfillment rules, legal terms, or time-limited availability that you don’t want mixed with other items.
- Use “one item per product” (built-in) when you want to cap quantity for a specific product but you’re fine with mixed carts.
If you need both at once for the same product, keep the snippet and also tick Sold individually. The snippet guarantees exclusivity; the setting guarantees quantity = 1.
These kinds of decisions — whether to restrict, automate, or simplify — mirror the same trade-offs I wrote about in Do You Trust Your Instincts? Making Smart WordPress Choices.


Leave a Comment