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 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 );
		wc_set_customer_auth_cookie( $user->ID );
	}
}

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

19 thoughts on “WooCommerce: Automatically Register Guest Checkouts

  1. 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”

  2. 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

  3. 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”?

  4. 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. 🙂

  5. 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

  6. 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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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