Yes, “manage stock” is a nice feature to make sure you don’t oversell a given product based on the stock you have in your warehouse. However, what if you also need to have a “daily sales limit” – say you can’t sell more than 3 of a given product ID in a given day?
This is an interesting functionality that is also helpful for you to learn how to get today’s orders, how to loop through the orders to find a specific product ID and sum its quantities, and finally how to use the woocommerce_is_purchasable filter to set if a product can be purchased or not (which means, the add to cart may or may not show). Enjoy!
PHP Snippet: Deny Selling if a Product Has Sold More Than X Today
/**
* @snippet Limit Daily Sales For Product ID
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_is_purchasable', 'bbloomer_not_purchasable_after_daily_limit', 9999, 2 );
function bbloomer_not_purchasable_after_daily_limit( $is_purchasable, $product ) {
$limit_product_id = 12345; // SET YOUR PRODUCT ID HERE
if ( $product->get_id() !== $limit_product_id ) return $is_purchasable;
// GET TODAYS ORDERS AND LOOP
$all_orders = wc_get_orders(
array(
'limit' => -1,
'date_created' => date( 'Y-m-d' ),
'return' => 'ids',
)
);
$count = 0;
foreach ( $all_orders as $all_order ) {
$order = wc_get_order( $all_order );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( $product_id && $product_id == $limit_product_id ) {
$count = $count + absint( $item['qty'] );
}
}
}
// LIMIT 3 DAILY SALES
if ( $count >= 3 ) return false;
return $is_purchasable;
}
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
WooCommerce: Automatically Update Cart on Quantity Change There is a lot of literature online that solves this UX problem – so in this article let’s see if I can give you a simplified, working, updated version. So, do you hate the “Update Cart” button too? Yes, the one you have to click after you update the quantity of a product in the […]
WooCommerce: Add to Cart Quantity Plus & Minus Buttons Here’s a quick snippet you can simply copy/paste or a mini-plugin you can install to show a “+” and a “-” on each side of the quantity number input on the WooCommerce single product page and Cart page. The custom code comes with a jQuery script as well, as we need to detect whether the […]
WooCommerce: Only Allow 1 Product in the Cart Here’s how to limit your WooCommerce Cart to just one product at a time. This simple solution can be used for many applications. For example, your store may only allow to buy one subscription at a time. On this same website, for example, customers can only purchase one product at a time so it’s easier […]
WooCommerce: How to Add a Custom Checkout Field Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
WooCommerce: Get Order Data (total, items, etc) From $order Object As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
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
8 thoughts on “WooCommerce: Limit Sales Of A Product Per Day”
Stefan Loots
Hi. Fantastic work.
Is there a way to limit the stock based on a day selected from a callender field? So I only allow 3 to be sold on the date entered. This will allow future purchases to be limited.
Use case: Say I have a theater, and I only have 3 seats available. I would like to sell 3 tickets every day. So if all tickets are sold for today. I can try for a different date.
Hello Stefan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Jack, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
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!
Hi. Fantastic work.
Is there a way to limit the stock based on a day selected from a callender field? So I only allow 3 to be sold on the date entered. This will allow future purchases to be limited.
Use case: Say I have a theater, and I only have 3 seats available. I would like to sell 3 tickets every day. So if all tickets are sold for today. I can try for a different date.
Hello Stefan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Can this be done 1 per customer id
Hi Jack, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I cant seam to get this to work. Is there way to limit it per product tag instead of id?
Did you use the exact same code and test it with a Simple product? If not, give me more context please, because it works for me.
In regard to the other question, of course – anything is possible
Interesting snippet. Thank you very much
You’re welcome!