defined( 'ABSPATH' ) || exit;
// 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 = $_SERVER['REMOTE_ADDR'];
// Check if the IP address has placed an order within the last 24 hours
if ( ip_address_has_order( $ip_address ) ) {
$errors->add( 'order_error', 'Sorry, you can only place one order per 24 hours.' );
}
}
function 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' => 'wc-completed', // Only consider completed orders
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => '_customer_ip_address',
'value' => $ip_address,
'compare' => '=',
),
array(
'key' => '_date_completed',
'value' => strtotime( '-24 hours' ),
'compare' => '>',
'type' => 'numeric',
),
),
);
$orders = get_posts( $args );
return ! empty( $orders );
}
/**
* Auto Complete all WooCommerce orders.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
// Register a new custom order status
function register_custom_order_status() {
register_post_status('wc-order-paid', array(
'label' => __('Order Paid', 'woocommerce'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Order Paid <span class="count">(%s)</span>', 'Order Paid <span class="count">(%s)</span>', 'woocommerce'),
));
}
add_action('init', 'register_custom_order_status');
// Add the new custom order status to WooCommerce
function add_custom_order_status_to_dropdown($order_statuses) {
$order_statuses['wc-order-paid'] = __('Order Paid', 'woocommerce');
return $order_statuses;
}
add_filter('wc_order_statuses', 'add_custom_order_status_to_dropdown');