Advertisement
vapvarun

Prevent Duplicate Orders in WooCommerce within One Hour

Jan 10th, 2024
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | Software | 0 0
  1. /**
  2.  * Title: Prevent Duplicate Orders in WooCommerce within One Hour
  3.  * Description: This code snippet prevents duplicate orders from being placed by the same user within an hour.
  4.  * It checks the orders made by the current user in the last hour before a new order is processed.
  5.  */
  6.  
  7. add_action( 'woocommerce_checkout_process', 'wbcom_prevent_duplicate_order_within_one_hour' );
  8.  
  9. /**
  10.  * Prevents a user from placing a duplicate order within one hour.
  11.  */
  12. function wbcom_prevent_duplicate_order_within_one_hour() {
  13.     // Get current user information
  14.     $current_user = wp_get_current_user();
  15.  
  16.     // Define the query parameters for recent orders
  17.     $args = array(
  18.         'customer_id' => $current_user->ID, // Orders by the current user
  19.         'date_created' => '>' . ( time() - HOUR_IN_SECONDS ), // Orders within the last hour
  20.         'status' => array( 'on-hold', 'processing', 'completed' ) // Only include these statuses
  21.     );
  22.  
  23.     // Retrieve the orders based on the specified criteria
  24.     $orders = wc_get_orders( $args );
  25.  
  26.     // Loop through each order and check for duplicates
  27.     foreach( $orders as $order ) {
  28.         // Additional checks can be added here, e.g., check total amount, items, etc.
  29.  
  30.         // Add an error notice and stop the checkout process
  31.         wc_add_notice( 'A similar order has been placed in the last hour. Please wait before placing a new order.', 'error' );
  32.         return;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement