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!
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 );
}
}
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.
Done, your feedback was awesome!
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.
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!
Works great, but will this work with the new HPOS-system too?
Yep!
I dont know why, but it doesnt work for me. It is not creating new ser from guest order…
Not sure, works for me!
It isn’t working for me either. These are my settings and the code
https://snipboard.io/IcoB4U.jpg
https://snipboard.io/hd9qmi.jpg
Ah, you need to enable “generate an account username” and “send a password reset”
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
There may be another set of code for that, please test the following (add it BEFORE the wc_update_new_customer_past_orders line):
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
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
A role which is not “customer”?
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?
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. 🙂
No prob at all!
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!
Hey Jovan, wc_update_new_customer_past_orders() function already does that
If this Snippet removes the Create account password on checkout, then how would the customer access their account?
Hi Brooke! wc_create_new_customer should already email account details to customer – please test and let me know
Hi, other than the regular invoice, nothing is emailed to the customer. Is there an update to the code?
You mean no password reset and new account emails?
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.
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!