WooCommerce: Automatically Register Guest Checkouts

A disclaimer first: please make sure this is legal in your country and also that your checkout visitors are aware they will become registered customers without explicit consent (i.e. without ticking a “create an account on this site” checkbox).

So, yeah, there is a way to turn guest checkouts into registered customer ones. Also, there is a neat WooCommerce function to bulk add all past guest orders to a new customer (wc_update_new_customer_past_orders).

Of course, “Allow customers to place orders without an account” must be enabled in your WooCommerce settings, otherwise you’re not allowing guest checkouts and the snippet will be irrelevant.

So, here’s the fix. Enjoy!

This screenshot requires a bit of context. With the snippet below, you automatically register guest users and sync their past guest orders as well. This means, if you wish, that you can totally disable the “Allow customers to create an account during checkout” WooCommerce setting because no matter what, user will be registered automatically. Hope that makes sense!

PHP Snippet: Automatically Register Guest Customers @ WooCommerce Checkout (and Programmatically Log Them In @ WooCommerce Thank You Page)

/**
 * @snippet       Register Guest Users @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_thankyou', 'bbloomer_register_guests', 9999 );

function bbloomer_register_guests( $order_id ) {
	$order = wc_get_order( $order_id );
	$email = $order->get_billing_email();
	if ( ! email_exists( $email ) && ! username_exists( $email ) ) {
		$customer_id = wc_create_new_customer( $email, '', '', array(
			'first_name' => $order->get_billing_first_name(),
			'last_name'  => $order->get_billing_last_name(),
		));
		if ( is_wp_error( $customer_id ) ) {
			throw new Exception( $customer_id->get_error_message() );
		}
		wc_update_new_customer_past_orders( $customer_id );
		wc_set_customer_auth_cookie( $customer_id );
	} else {
		$user = get_user_by( 'email', $email );
		wc_update_new_customer_past_orders( $user->ID );
	}
}

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

  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: Disable Payment Gateway by Country
    You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]

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

25 thoughts on “WooCommerce: Automatically Register Guest Checkouts

  1. I noticed a significant security concern with this code as written.
    The wc_set_customer_auth_cookie on the else conditions means that if the email or username already exists, it won’t create the account again of course, but it also does the wc_set_customer_auth_cookie which effectively logs the user in as that account.

    This means if I know the email address of ANY Administrator account and place an order with that email, this code will gleefully log me into WP as Administrator, at which point I can go in and wreak havoc if I desire. The wc_set_customer_auth_cookie() should be removed from the ELSE condition to secure that, which would force the user to login after checkout if they need to reach My Account or something.

    It’s cool that the code can automatically login a user whose account WAS just created for them, which smoothes the handover to My Account duties for download retrieval and whatnot. But if the account already exists, and we’re mapping this order to it, the user should already know how to login as that user, and can use Forgot Password if they don’t – but under no circumstances should you be using wc_set_customer_auth_cookie() to login a completely unauthenticated user whose account you did not just create. So, delete that 2nd occurrence of wc_set_customer_auth_cookie() , which is under that ELSE condition.

    1. Done, your feedback was awesome!

  2. Hello.
    How can register guest checkout as customer with mobile number instead of email. I don’t want to provide email id on checkout page.

    1. Hello, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Works great, but will this work with the new HPOS-system too?

  4. I dont know why, but it doesnt work for me. It is not creating new ser from guest order…

    1. Not sure, works for me!

      1. It isn’t working for me either. These are my settings and the code
        https://snipboard.io/IcoB4U.jpg
        https://snipboard.io/hd9qmi.jpg

        1. Ah, you need to enable “generate an account username” and “send a password reset”

  5. Hello, I noticed that although the new orderer receives a user account, the data from the order (name, address, city, zip code, etc.) is not transferred to the master data of the user account. These are only stored in the order, i.e. the user must enter his data again with the next order. Is this correct or a bug with me? Thanks for the great snippet. Best regards, Oliver

    1. There may be another set of code for that, please test the following (add it BEFORE the wc_update_new_customer_past_orders line):

      if ( $customer_id ) {
         $customer = new WC_Customer( $customer_id );
         if ( $order->get_billing_first_name() && '' === $customer->get_first_name() ) {
            $customer->set_first_name( $order->get_billing_first_name() );
         }
         if ( $order->get_billing_last_name() && '' === $customer->get_last_name() ) {
            $customer->set_last_name( $order->get_billing_last_name() );
         }
         if ( is_email( $customer->get_display_name() ) ) {
            $customer->set_display_name( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() );
         }
         $customer->save();
      }
      

      This should save WordPress user name, last name and display name. If this works let me know and I’ll add more code to save also billing & shipping

  6. Hi Rodolfo,

    I’ve set this up in a child theme, but how do I get it to kick in so it creates an wp role for the existing guest customers?

    David

    1. A role which is not “customer”?

  7. Hello,

    This works, but I noticed it slows down WooCommerce a LOT. When I place an order, it takes forever to get to the checkout and then confirm the order. I assume he is searching all customers records first trying to find the matching email? If I remove the snippet, the speed is back to normal. Any ideas if this can be avoided?

    1. Let me update. I ran it again, and it seems like the lag was not because of this snippet. It might have been a temporary thing on the server and the snippet does not seem to slow down anything. Sorry about that. 🙂

  8. Thanks for This! Really helps a lot! I just want to know if you have a related tutorial where order from guest checkout can be sync if customer created an account? Thank you!

    1. Hey Jovan, wc_update_new_customer_past_orders() function already does that

  9. If this Snippet removes the Create account password on checkout, then how would the customer access their account?

    1. Hi Brooke! wc_create_new_customer should already email account details to customer – please test and let me know

      1. Hi, other than the regular invoice, nothing is emailed to the customer. Is there an update to the code?

        1. You mean no password reset and new account emails?

          1. My friend Rodolfo, thank you very much for your code.

            I would like to know if there is a possibility of using this code to register the customer after placing the order.

            I’ll explain: I use an external professional transparent checkout that connects to woocommerce. When my customer places an order, this checkout registers an order in my woocommerce, but the buyer is only registered as a Visitor, so is there any other place that I can insert this snippet, since the external checkout does not load the woocommerce_thankyou.

            1. Hello André, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

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 *