WooCommerce: Limit Sales Of A Product Per Day

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!

My goal is to hide the add to cart and deny purchases of this product if today I’ve sold more than 3. Find the WooCommerce snippet below!

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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;
	
}

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

4 thoughts on “WooCommerce: Limit Sales Of A Product Per Day

  1. I cant seam to get this to work. Is there way to limit it per product tag instead of id?

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

  2. Interesting snippet. Thank you very much

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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