I want to allow only a specific number of orders per product per day on my shop. Any idea which hook do I have to add?
Thanks
Advertisement
Answer
The code below uses the product ID to get the total quantity for the current day. If this is higher than or equal to the $dailyOrderLimit
that you set (in my example, it’s 10), the product cannot be purchased.
add_filter('woocommerce_is_purchasable', 'preventPurchaseIfDailyOrderLimitReached', 10, 2); function preventPurchaseIfDailyOrderLimitReached($is_purchasable, $product) { $dailyOrderLimit = 10; $productId = $product->get_id(); $quantityOrderedToday = getDailyOrderAmount($productId); if ($quantityOrderedToday >= $dailyOrderLimit) { $is_purchasable = false; } return $is_purchasable; } function getDailyOrderAmount($productId) { global $wpdb; $today = date('Y-m-d'); $result = $wpdb->get_results("SELECT sum(product_qty) as quantity_ordered_today FROM {$wpdb->prefix}wc_order_product_lookup where product_id= {$productId} and date_created > '{$today} 00:00:00';"); return $result[0]->quantity_ordered_today; }
Code is tested and works. Add it to the functions.php
file of your child theme.
If you feel that my answer helped you, you could accept my answer.