How to limit purchases to one per order

It’s not unusual that products need to be handled singularly in an order, or that they can’t be sold in more than one unit.

WooCommerce covers the second case so you can limit customers to buy only one unit of a specific item per order. The first case instead is not covered by default, but it’s still possible to handle it with some custom coding.

Let’s see how to do both.

Limit purchases to one product per order

This scenario is not covered by WooCommerce by default, but it can be done with custom code. The goal is to allow customers to buy only one product per order.

Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
if ( 31 !== $product_id ) {
return $passed_validation;
}
if ( WC()->cart->get_cart_contents_count() >= 1 ) {
wc_add_notice( __( 'This product cannot be purchased with other products. Please, empty your cart first and then add it again.', 'woocommerce' ), 'error' );
return false;
}
return $passed_validation;
}
view raw functions.php hosted with ❤ by GitHub

This snippet will not allow customers to add more than one item to the cart, showing them an error message and forcing them to empty the cart to order or to complete the current order first.

To choose which product to limit you need to change the product ID on line 3 of this snippet. In the example, the ID is 31. If you need to limit multiple products, change line 3 with this code instead:

if ( ! in_array( $product_id, array( 31, 32, 33 ) ) ) {

Limit purchases to one item per order

This is the second scenario and it covers any case where you don’t want your customers to add more units of the same product to the cart, but still allow them to buy multiple different products to the cart.

As I said before, this is already covered by WooCommerce, and to do it you need to edit a product in Products > All Products and click on the Inventory tab, then select the option Sold Individually, like shown in this screenshot:

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

After selecting this option, customers will not be able to buy more than one of the item in the same order, but they still can do it in different orders.

Also, if you are you looking for how to limit purchases by role I wrote about that in this article!

Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me writing more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Do you prefer using cryptocurrency? I’ve got you covered! You can donate at the following wallets:

  • BTC: bc1q87nxpdpqw323ccmerrzyeffs72wnxpw93x7pzk / 17Gtc5e8yoh2LZzokoyK8P5DWbToBwztok
  • ETH: 0xA5C89Be1df2896C3942DBEf751cCeacC7929388b


More Posts That You Might Like…


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. 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!

  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. woo on wordpress Avatar

    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 Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: