// Hook into the WooCommerce checkout process
add_action( 'woocommerce_after_checkout_validation', 'ip_order_limit_check', 10, 2 );
function ip_order_limit_check( $data, $errors ) {
// Get the customer's IP address
$ip_address = WC_Geolocation::get_ip_address();
// Check if the IP address has placed an order within the last 24 hours
if ( the_ip_address_has_order( $ip_address ) ) {
$errors->add( 'order_error', 'Sorry, you can only place one order per 24 hours.' );
}
}
function the_ip_address_has_order( $ip_address ) {
// Query WooCommerce orders to check if the IP address has placed an order within the last 24 hours
$args = array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-completed' ), // Consider both processing and completed orders
'date_created'=> strtotime( '-24 hours' ),
'date_created_compare' => '>',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_customer_ip_address',
'value' => $ip_address,
'compare' => '=',
),
),
);
$orders = get_posts( $args );
return ! empty( $orders );
}