I’m looking for a restriction if a guest user has bought a specific product 2 times in a week he must not be able to make another purchase of same product.
Im looking to apply this restriction based on guest user’s email.
I’ve seen many posts related to that but all focus on registered users however i want to apply this restriction for guest users.
This is the code I am currently using, unfortunately without the desired result
function my_ip_checker() {
$last_order = get_posts(array(
//'date_created' => '>=' . (time() - 86400), time in seconds
'meta_key' => '_billing_email',
'meta_value' => sanitize_email( $_POST['cb_email'] ),
'post_type' => 'shop_order',
'post_status' => array('wc-processing', 'wc-completed')
));
if($last_order->total > 1) {
wc_add_notice('Too many orders in the last 24 hours. Please return later.', 'error');
}
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0);
Any help is appreciated.
2
Answers
For a restriction for a specific variable product per week i used:
wc_get_orders
andWC_Order_Query
Thanks to the 1st answer above for providing me the base to start implementation. Any further improvements in my code are welcomed.
For a restriction for a specific product per week you could use:
wc_get_orders
provide a standard way of retrieving orders – wc_get_orders and WC_Order_QueryTo apply this only for guest users:
Replace
With