How to automatically register guest users on checkout in WooCommerce

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.

Thus, our goal for today is to provide a third option by allowing automatic user account creation for guest users, via PHP coding.
This article will show you a way of doing that. We’ll allow guest checkout but we’ll also automatically create a user profile in the background. You will also have access to the user’s custom fields and you’ll have them marked as Guests.

These are the main blocks of our custom code to work:

  • Preparation
  • 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 WooCommerce. You can do that under WooCommerce > Settings > Checkout > Checkout Process > Enable guest checkout:

Guest Checkout

The second step is a nice addition to make sure your code won’t be lost in future 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. In order to do that, we’ll add a snippet that takes action when you are on the thank you page. In this way you’ll see this code even if the order payment is still pending.

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:

 

Creating a user account

Once we know that the current user is a guest we can create a new 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.

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

 

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.

This is the code to do it

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:

 

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:

Guest custom field mapping

 

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.

WooCommerce filter guest users

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.
Filter guest WordPress users based on their ordered products
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.
Filter guest registered users based on lastest activity
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.
Export guest users
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.
Filter users based on billing country
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.
Save segments for later use

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:

We hope you enjoyed this article, and see you next time!