Free products are a fantastic way to engage users and promote your store, but they also attract spam orders especially from bots targeting your free downloads. If you’ve ever woken up to dozens of fake orders overnight, you know exactly how frustrating this can be: clogged analytics, flooded emails, and wasted time.
In this post, I’ll show you a simple, lightweight PHP snippet to limit how many times a free product can be ordered per day, specifically for products in a certain category. Once the limit is reached, the product becomes temporarily unavailable with a clear “come back tomorrow” message.
Why Free Products Are Vulnerable to Spam
Free products in WooCommerce are frequently targeted by bots because they don’t require payment. The results?
- Multiple fake “Completed” orders in a single day
- Flooded admin emails
- Inaccurate analytics and reports
- Disrupted workflow for legitimate customers
How to Limit Daily Orders for Free Products
The solution is to count the number of times a free product has been purchased in a day and prevent further purchases once a daily limit is reached. This snippet targets all free products in a specific product category (e.g., “plugins”) and makes them temporarily non-purchasable.
PHP Snippet: Limit Daily Orders for Free WooCommerce Products
Add the following snippet to your child theme’s functions.php
file or a code snippet plugin like Code Snippets:
/**
* @snippet Limit Spam WooCommerce Orders for Free Products
* @tutorial https://example.com/tutorial
* @author Your Name
* @compatible WooCommerce 9+
* @community https://example.com/community
*/
add_filter( 'woocommerce_is_purchasable', 'limit_daily_sales', 10000, 2 );
function limit_daily_sales( $is_purchasable, $product ) {
// Only target free products in the "plugins" category
if ( floatval( $product->get_price() ) > 0 ) return $is_purchasable;
if ( ! has_term( 'plugins', 'product_cat', $product->get_id() ) ) return $is_purchasable;
$product_id = $product->get_id();
$today = date( 'Y-m-d' );
$count = 0;
// Get all orders created today
$orders = wc_get_orders([
'limit' => -1,
'date_created' => $today,
'status' => [ 'processing', 'completed' ],
'return' => 'ids',
]);
foreach ( $orders as $order_id ) {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ) {
$item_product_id = $item->get_product_id();
if ( $item_product_id && $item_product_id === $product_id ) {
$count += absint( $item->get_quantity() );
}
}
}
// Daily limit reached: make product not purchasable
if ( $count >= 5 ) {
add_filter( 'woocommerce_get_price_html', 'free_plugin_sold', 9999, 2 );
return false;
}
return $is_purchasable;
}
function free_plugin_sold( $price_html, $product ) {
if ( floatval( $product->get_price() ) == 0 && has_term( 'plugins', 'product_cat', $product->get_id() ) ) {
return '<ul class="woocommerce-error"><li>This free plugin is out of stock for today. Please come back tomorrow!</li></ul>';
}
return $price_html;
}
How This Snippet Works
- Checks if the product is free (
$product->get_price() == 0
) - Filters by product category (
plugins
in this case) - Counts the quantity ordered today using WooCommerce orders
- Disables purchasing once the limit (5 per day in this snippet) is reached
- Displays a styled error message for users visiting the product page
Benefits
- Stop bot attacks on free products
- Prevent spam orders and fake analytics
- Lightweight, no extra plugins needed
- Customizable: change the category or daily limit as needed
Final Thoughts
Free products are powerful marketing tools, but they need protection from abuse. With this snippet, you can limit daily orders per product, keep your analytics clean, and reduce admin spam without installing additional plugins.