How to register WooCommerce guest users on checkout automatically
Allowing guest checkouts in WooCommerce are great for your sales conversions. It allows customers to checkout quickly without the need to create and login to their account. But once you don’t create accounts for your users, you’ll lose a lot of data and potential customer analysis opportunity.
In an ideal world we’d be able to allow users to quickly order, but also we’d offer them an account once they enter their data. This will allow you to keep in touch with your users and build a relationship with them. Unfortunately, currently there isn’t a way of doing this by default with WooCommerce and we couldn’t find any free plugin that does this.
We’ll allow guest checkout but we’ll also automatically create a user profile in the background.
Our goal for today’s tutorial is to provide a third option by allowing automatic WooCommerce . for guest users, via some custom PHP code. We’ll allow but we’ll also automatically create a user profile in the background and create account after automatically without the need for a separate . We’ll use the data from the of your to replace the and create
These are the main blocks of our custom code to work:
- Preparation and settings
- Detect if the current order is from a guest user
- Create a user account
- Add custom fields to the user account
- Link past orders to the new user account
- Reading the custom fields data
- Wrapping up
Let’s get right into it!
Preparation
In order to add this code we’ll need two other factors. First is enabling the guest checkout in our WooCommerce store. You can do that under WooCommerce > Settings > Checkout > Checkout Process > Enable guest checkout:
The second step is a nice addition to make sure your code won’t be lost in future WordPress theme updates. We’ll add your code in a child theme. If you don’t know what a Child Theme is, we recommend checking the WordPress guides about child themes. You can alternatively create a plugin and add your code to the plugin, but since using child themes is easier and it’s most likely that you already have one installed, we are going to use this method.
You don’t need to code anything though in order to create a child theme – just install a plugin such as the One Click Child Theme. Once you’ve created a child theme we’ll use your child theme’s functions.php file to add our code.
Now let’s start coding!
Detecting if the current order is from a guest user
The first task of our code is detecting if the current order is coming from a guest user. We can do this during the WooCommerce checkout process by using different WooCommerce pages. In order to do that, we’ll add a snippet that takes action when the customer is on the thank you page. This page is shown to the customer right after the WooCommerce checkout page. In this way you’ll execute this code even if the order payment is still pending (depending on the payment method).
Since there is no direct function to detect if a user is a guest, we’ll check if there is already a user registered with this email.
This is the code to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function wc_register_guests( $order_id ) { // get all the order data $order = new WC_Order($order_id); //get the user email from the order $order_email = $order->billing_email; // check if there are any users with the billing email as user or email $email = email_exists( $order_email ); $user = username_exists( $order_email ); // if the UID is null, then it's a guest checkout if( $user == false && $email == false ){ // perform guest user actions here } } //call our wc_register_guests() function on the thank you page add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 ); |
Creating a user account
Once we know that the current user is a guest we can create a new WooCommerce account. For that we’ll simply use the email as username and email, and generate a password. You can use any password at this moment, as the user won’t get access to it directly.
1 2 3 4 5 |
// random password with 12 chars $random_password = wp_generate_password(); // create new user with email as username & newly created pw $user_id = wp_create_user( $order_email, $random_password, $order_email ); |
It would be a good idea at this point to send an email to your user. This email can tell them that even though they have ordered as guest, they can still create a password for their account. In this way they’ll be able to place new orders or check the status of the current ones. For the password reset function you can simply use WordPress password reset mechanics. Just send them a link to yoursite.com/wp-login.php?action=lostpassword
. Please note that although we create the account after checkout, the payment might not have been confirmed yet. You can also create account after the payment has been completed, but that process will be dependent on your specific setup, WooCommerce settings and payment process.
Add custom fields to the user account
WooCommerce has a lot of custom fields added to your users. It’s a good idea to add them as well, so you can read this data later and autofill in future orders. Also, it’d be nice to add a custom field to mark these users as previously guest users. You can use the same technique to load other custom data from your Woocommerce checkout form, like data from WooCommerce custom checkout fields for example, and store it in your customer account.
This is the code to do it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//WooCommerce guest customer identification update_user_meta( $user_id, 'guest', 'yes' ); //user's billing data update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 ); update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 ); update_user_meta( $user_id, 'billing_city', $order->billing_city ); update_user_meta( $user_id, 'billing_company', $order->billing_company ); update_user_meta( $user_id, 'billing_country', $order->billing_country ); update_user_meta( $user_id, 'billing_email', $order->billing_email ); update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name ); update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name ); update_user_meta( $user_id, 'billing_phone', $order->billing_phone ); update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode ); update_user_meta( $user_id, 'billing_state', $order->billing_state ); // user's shipping data update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 ); update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 ); update_user_meta( $user_id, 'shipping_city', $order->shipping_city ); update_user_meta( $user_id, 'shipping_company', $order->shipping_company ); update_user_meta( $user_id, 'shipping_country', $order->shipping_country ); update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name ); update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name ); update_user_meta( $user_id, 'shipping_method', $order->shipping_method ); update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode ); update_user_meta( $user_id, 'shipping_state', $order->shipping_state ); |
Link past orders to the new user account
The last step missing in our code is linking past orders to this new account. In this way your users will be able to manage their orders via the “My account” page.
Thankfully WooCommerce got us covered. They have a function to this task in particular. You can use this code for that:
1 2 |
// link past orders to this newly created customer wc_update_new_customer_past_orders( $user_id ); |
Unlock the Full Potential of Your WooCommerce Customer Data
Reading the custom fields data
Now, that we have created all the custom fields, we can just read these fields’ data by using something such as Users Insights. For example, you can list all guest users, once you map that newly created custom field:
Just filter all users with your value (in our case it’s “Yes”). You can also filter out all users that aren’t guests, by using a “Guest Checkout is not set” filter.
Creating advanced filters with your customer data
In addition to the custom fields created in this tutorial, we can use a lot more filters. For instance, it is possible to filter all users who have bought a certain product and who are guests.
Since the account creation is automatic, maybe these users haven’t visited your store in a while. You can check if that’s the case with the “last seen filter”, which can even be combined with the “last order”. In this way, we can check users who have ordered and haven’t come back.
As usual, it is a good idea to develop a better customer relationship. For that, you can simply export these users, and contact them using MailChimp. This allows you to remind them of your store and maybe send them a good offer.
Furthermore, it is possible to filter users based on their location. In addition to the GeoLocation API, you can use the WooCommerce location information. The user fields such as billing country and city are available as columns and filters in Users Insights. Thus, it is possible to filter all users from a specific billing location.
Maybe you can get yourself repeating the same searches over and over. In these cases, the segment feature may come in handy. For instance, if you report all US users who have placed an order last month, you can save that segment. This allows you to repeat the same search with a single click.
Wrapping up
Today we looked into the limitations of a pure guest checkout. We proposed a solution with a bit of custom code. This is the final code you can copy & paste into your child theme’s functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
function wc_register_guests( $order_id ) { // get all the order data $order = new WC_Order($order_id); //get the user email from the order $order_email = $order->billing_email; // check if there are any users with the billing email as user or email $email = email_exists( $order_email ); $user = username_exists( $order_email ); // if the UID is null, then it's a guest checkout if( $user == false && $email == false ){ // random password with 12 chars $random_password = wp_generate_password(); // create new user with email as username & newly created pw $user_id = wp_create_user( $order_email, $random_password, $order_email ); //WC guest customer identification update_user_meta( $user_id, 'guest', 'yes' ); //user's billing data update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 ); update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 ); update_user_meta( $user_id, 'billing_city', $order->billing_city ); update_user_meta( $user_id, 'billing_company', $order->billing_company ); update_user_meta( $user_id, 'billing_country', $order->billing_country ); update_user_meta( $user_id, 'billing_email', $order->billing_email ); update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name ); update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name ); update_user_meta( $user_id, 'billing_phone', $order->billing_phone ); update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode ); update_user_meta( $user_id, 'billing_state', $order->billing_state ); // user's shipping data update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 ); update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 ); update_user_meta( $user_id, 'shipping_city', $order->shipping_city ); update_user_meta( $user_id, 'shipping_company', $order->shipping_company ); update_user_meta( $user_id, 'shipping_country', $order->shipping_country ); update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name ); update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name ); update_user_meta( $user_id, 'shipping_method', $order->shipping_method ); update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode ); update_user_meta( $user_id, 'shipping_state', $order->shipping_state ); // link past orders to this newly created customer wc_update_new_customer_past_orders( $user_id ); } } //add this newly created function to the thank you page add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 ); |
We hope you enjoyed this article, and see you next time!