/**
* Title: Prevent Duplicate Orders in WooCommerce within One Hour
* Description: This code snippet prevents duplicate orders from being placed by the same user within an hour.
* It checks the orders made by the current user in the last hour before a new order is processed.
*/
add_action( 'woocommerce_checkout_process', 'wbcom_prevent_duplicate_order_within_one_hour' );
/**
* Prevents a user from placing a duplicate order within one hour.
*/
function wbcom_prevent_duplicate_order_within_one_hour() {
// Get current user information
$current_user = wp_get_current_user();
// Define the query parameters for recent orders
$args = array(
'customer_id' => $current_user->ID, // Orders by the current user
'date_created' => '>' . ( time() - HOUR_IN_SECONDS ), // Orders within the last hour
'status' => array( 'on-hold', 'processing', 'completed' ) // Only include these statuses
);
// Retrieve the orders based on the specified criteria
$orders = wc_get_orders( $args );
// Loop through each order and check for duplicates
foreach( $orders as $order ) {
// Additional checks can be added here, e.g., check total amount, items, etc.
// Add an error notice and stop the checkout process
wc_add_notice( 'A similar order has been placed in the last hour. Please wait before placing a new order.', 'error' );
return;
}
}
Comments