Skip to main content

Woocommerce Prevent Duplicate Orders

The purpose of this code is to create a 3-minute window after an order is placed, during which the same email address cannot be used to place another order. This helps prevent accidental duplicate orders that might occur due to double-clicking or refreshing the page during checkout.

If a user tries to place another order within this 3-minute window, they'll see an error message directing them to check their existing orders or wait before placing a new one.

This is a common practice in e-commerce to prevent unintentional duplicate transactions, improving the user experience and reducing potential order fulfillment issues.

 /** PREVENT DUPLICATE ORDERS */
add_filter('woocommerce_defer_transactional_emails', '__return_true' );
add_action( 'woocommerce_after_checkout_validation', 'forbid2orders', 10, 2 );
function forbid2orders( $fields, $errors ){
	$c_user_email = $fields['billing_email'];
	$c_transient_key = 'create_order_'.$c_user_email;
	$create_order = true;
	if(false === ($c_transient_results = get_transient($c_transient_key))){
		$c_transient_results = $fields['billing_email'];
		set_transient($c_transient_key, $c_transient_results, 3 * MINUTE_IN_SECONDS );
	}else{
		if($c_transient_results == $fields['billing_email']){
			$errors->add( 'validation', 'An order has already been created.  Please check your <a href="/my-account/orders/">Orders</a> or wait a few minutes to place a new one.' );
			
		}
	}
}

This code is implementing a mechanism to prevent duplicate orders in a WooCommerce-based WordPress site. Here's a breakdown of what the code is doing:

  1. Deferring transactional emails:

    add_filter('woocommerce_defer_transactional_emails', '__return_true' );
    

    This line defers the sending of transactional emails in WooCommerce, which can help prevent premature email notifications.

  2. Adding a custom validation hook:

    add_action( 'woocommerce_after_checkout_validation', 'forbid2orders', 10, 2 );
    

    This adds a custom function forbid2orders to the WooCommerce checkout validation process.

  3. The forbid2orders function:

    • It extracts the customer's email from the checkout fields.
    • Creates a unique transient key based on the email.
    • Checks if a transient with this key already exists:
      • If it doesn't exist, it creates a new transient with the email, lasting for 3 minutes.
      • If it does exist and matches the current email, it adds an error message to prevent a duplicate order.

The purpose of this code is to create a 3-minute window after an order is placed, during which the same email address cannot be used to place another order. This helps prevent accidental duplicate orders that might occur due to double-clicking or refreshing the page during checkout.

If a user tries to place another order within this 3-minute window, they'll see an error message directing them to check their existing orders or wait before placing a new one.

This is a common practice in e-commerce to prevent unintentional duplicate transactions, improving the user experience and reducing potential order fulfillment issues.