mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
34 lines
977 B
Text
34 lines
977 B
Text
add_filter( 'woocommerce_is_purchasable', 'wc_not_purchasable_after_daily_limit', 9999, 2 );
|
|
|
|
function wc_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;
|
|
|
|
}
|