mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-01 11:52:25 +08:00
https://stackoverflow.com/questions/69326107/how-to-set-a-maximum-limit-in-woocommerce-orders-per-item-per-day/69335419
23 lines
762 B
Text
23 lines
762 B
Text
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;
|
|
}
|