Nicola Mustone

Support Lead @ Automattic


Leadership, web, programming. Short essays and hands-on guides, focused on results, not hype.


How to Limit Purchases to One Per Order

2–3 minutes
hand holding event vip access badges

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:

  1. Go to Products → All Products.
  2. Edit the product.
  3. 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.

Inventory tab on the Edit Product screen, showing the Sold Individually option.

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.

Cite This Essay

Use this ready-formatted reference in your work or research notes.


20 responses to “How to Limit Purchases to One Per Order”

  1. hi in this condition i need to add redirect link

  2. From what i noticed in the code above, you can limit the order by selecting product id. What if you have so many products and you want to limit all the products purchase to be per order (individually).

  3. This is helpful definitely, moreover I am looking for something which can only allow adding one product in cart and no other products in the cart. Is this possible?

  4. This codes only works one way.

    So if i have an item in the cart, say item “ID230”. Then i try add the item “ID31” (as in example) the error message shows.

    But if i have item “ID31” in my cart already then i add other items, it allows me to do so.

    1. Yes , This code has some bug with logic. 🙁

  5. The array is not working for me, when i implement the code it just blankets 1 product per order

  6. How does this work on Multisite where it has to apply only to a single site store?

  7. bruceandmrgiggles Avatar
    bruceandmrgiggles

    Hi, can this be possible for categories instead of product IDs? Limit purchase to one product per order when the product is in a specific category? What I wanna see is the qty field not showing on the product page and on the cart page. Thanks for this, this is really helpful!

    1. Hello there,
      Technically yes, it is possible. You have the product ID, so you can check what categories it belongs to with `wp_get_post_categories` (https://developer.wordpress.org/reference/functions/wp_get_post_categories/) and then change the conditions to work on the category instead of the ID.

  8. Hi!
    Great post. It worked perfect.
    Is it possible to limit the purchase but without activating the error message?
    Thank you!

    1. Hello,
      Not that I can think of. The error message can be removed but it needs further custom code or template edits.

  9. Great code. Well done!

    1. Thank you!

  10. It does not work for us the snippet for multiple products. Nothing happens and it is still possible to purchase all the products together, instead of one for each purchase.

    1. Thanks for the report FC Lugano. I fixed the multiple products snippet. You can check the change, a ! was missing.

      1. Hey! I tried to use this code and it works if there are products in your cart already and you try and add the restriced product, it says empty the cart first. If you have the restricted product in your cart FIRST it allows you to then add more items and checkout. Any idea why this happens? It pretty much defeats the object of the code

  11. it is not working

  12. Wow! so helpful. and all this time later 😉

  13. Thanks! I use the code snippet to allow only one product per order in my WooCommerce shop. This is because stock can be in different warehouses, therefore it’s not possible to combine them in one order. Though it’s possible that a customer wants to order multiple products that are located in the same warehouse. It would be convenient to allow those orders with multiple products. I use an product attribute (pa_warehouse) to define where the product is located. Would it be possible in your opinion to allow multiple products in one order if the product attribute is the same value? What would the code snippet look like?

  14. It’s work for in array product add later , If you add other not array ID Product first , You can add the other product into cart.

Leave a Comment

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading