WooCommerce: Allow Guest Checkout For Existing Customers

In a typical WooCommerce setup, if you disable Guest Checkout via the settings, all customers are required to either log into an existing account or register a new one during checkout. This ensures every order is tied to a user account – great for store management, but not always ideal for user experience.

The problem? Returning customers who aren’t logged in will be forced to remember their credentials or manually reset their password, just to place another order. And if they skip login and try to check out with their email, they’ll get the dreaded “An account is already registered with your email address” error – and the order won’t go through.

Thankfully, there’s a way to bypass all that. With a simple PHP snippet, you can allow users to check out as guests if their billing email matches an existing customer. WooCommerce will assign the order to their account – no login required!

This is what WooCommerce customers see if guest checkout is disabled, and they try to check out without logging in and they’ve ordered before.

PHP Snippet: Let Existing Customer Check Out as Guest & Assign Order To Customer

Please note: clearly, guest checkouts must be disabled in your store, otherwise this snippet makes no sense:

This snippet checks if the billing email entered at checkout matches an existing WooCommerce customer. If it does, it quietly tells WordPress to treat the customer as “logged in” for just that request — without actually logging them in or setting a cookie.

This prevents WooCommerce from trying to create a new account (which would normally fail with a “email already exists” error), and allows the checkout to continue smoothly.

The order is then automatically assigned to the existing customer account. No login forms, no password resets – just a smoother experience for returning customers!

/**
 * @snippet       Allow Guest Checkout For Existing WooCommerce Customers
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_checkout_process', 'bbloomer_faux_login_for_existing_email' );

function bbloomer_faux_login_for_existing_email() {	
	if ( is_user_logged_in() ) return;
	if ( empty( $_POST['billing_email'] ) ) return;
	$email = sanitize_email( $_POST['billing_email'] );
	if ( email_exists( $email ) ) {
		$user = get_user_by( 'email', $email );		
		if ( $user ) {			
			wp_set_current_user( $user->ID );			
		}		
	}	
}

This snippet hooks into the WooCommerce checkout process using the woocommerce_checkout_process action.

It first checks if the user is already logged in – if so, it does nothing. Then it grabs the billing email from the checkout form and sanitizes it. If that email matches an existing WordPress user, it uses wp_set_current_user() to temporarily set the user context for the current request.

This tricks WooCommerce into thinking the customer is logged in just for the duration of the checkout, so the order gets assigned to the right account without showing errors or requiring a password. The customer stays logged out after purchase.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Allow Guest Checkout For Existing Customers

  1. But isn’t that a security risk. The chance for it to happen is nearly zero, but assume there is a typo is the mail submitted during the new_order, and previously such a user existed, then the new order will link to that user. By default Woo sends an email, causing confusion for the first customer.

    So the user naturally assumes, either their account was hacked, or its a scammer trying to run a con.

    1. Of course – it’s up to you to make it more secure if this is a risk for your store. In my case, it’s mostly free products or very expensive ones, so people will either create a new account (free products) or be 100% sure they’re using the correct billing email.

      Many store owners send an email with a “magic link” in order to verify that the email is legit, for example, so that could be a good compromise.

  2. I think using this snippet can potentially harm my business. As this will let any anonymous person to place orders against any of the existing customers by entering their email. And this led to increase in fake orders.
    For example: Let say John is an existing customer on example.shop, if I (a suspicious or bad person), place an order by using John’s email, and when the delivery arrives at the address, John will be oblivious of the situation(yes customers get order confirmation mail when an order is placed, but many people sometimes miss emails).
    Ultimately the vendor will have to bear the delivery/return charges, which is a loss for them.
    So, think before using this feature.

    1. Of course, this strictly depends on the kind of WooCommerce site you run. In my case, there is no problem because I use rate limiting and also have a custom feature in place to prevent duplicate orders and limit failed ones, so it’s never a problem for me.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *