How to create WooCommerce customers programmatically

In today’s guide we are going to show you how to create WooCommerce customers programmatically. This is useful if you want to import customers from external services like email marketing software or spreadsheets. Our goal for this tutorial is to create customers, add customer data, change customer data and make sure that this works correctly within the WooCommerce framework.

WooCommerce is a great tool for WordPress developers. It has all you need to create an online store. In addition to what users can see, WooCommerce has a robust set of functions and modules that can be used to interact with it.

One of the many modules for WooCommerce is the WooCommerce user registration and authentication module. This module does both user registration and authentication and it can be carried out from within the WordPress admin area. The functions are quite simple to use making it quick and easy to setup fully functional registration with email verification for your users. This article will take you through how to set it up.

woocommerce create customer programmatically

In its core WooCommerce customers are just WordPress users. But if we were to use only the WordPress functions to create and manipulate customers, we would be very limited.

Although WooCommerce is very flexible, we can still create our own reusable functions to do exactly what we need. Thus, we can create specific functions to create customers coming from an external software, to create customers from guest checkouts, to assign specific data to your customers.

Therefore, our goal today is to explore the WooCommerce functions, create our own functions and overall understand how to programmatically create WooCommerce customers.

Let’s dive right into it!

How to create a WooCommerce customer with PHP

As we said before, all WooCommerce customers are WordPress users. Then, the very first step in our process is to create a WordPress user.

You can use the WP function for that, wp_create_user, or you can use the special WC function, wc_create_new_customer:

 

The only difference between these 2 is that the first one creates a WordPress user with the default WP role defined in your site. The second one creates a new user with the “customer” role automatically, regardless of what you have as your default user registration role.

But there’s an important step to take here. We need to add a “flag” to differentiate the WooCommerce customers programmatically created from the self-registered customers. This allows us to quickly debug issues and to fix errors. In our code, we add the custom user field “programmatically” to do that.

This is the second version of our customer creation code:

This function returns either the user ID or a WP error. Now let’s make some changes to our users.

How to programmatically add orders to WooCommerce Customers

There are 2 main uses to adding WooCommerce orders to customers.

  1. Assign past orders to new users (previously ordered as guests)
  2. Assign new orders to new users (programmatically creating WooCommerce orders)

The first use case is quite interesting. Imagine you have guest orders in your store, but you are importing customers from somewhere else. Then it is useful to assign past orders that had no users attached to them to the correct customer.

And you can do this with just one line:

 

This function runs through all your orders and check if the customer ID provided should be assigned to any of them. If they should, the order is updated.

Then we have the case for creating new orders for your new users. This happens if you are migrating your orders along with your customers from a different plugin or software.

The custom order creation deserves an article by itself, but you can do it by using the wc_create_order function.

It allows us to create a new order object and add the required fields data to it. Let’s create a simple custom order to our new user with the following code snippet:

 

This function creates a new order with our desired data and it assigns that order to our existing user.

How to assign a role to WooCommerce Customers

Once we run that function, we create a new user with the WordPress user role set as “customer” role. But maybe you have different roles that you want to use. Thus, it’s handy to have a way to edit that information as well.

One thing to keep in mind here that we switch back and forth between the WC_Customer and the WP_User classes. Both use the same $id, but they provide a different set of functions.

Some functions, like editing user roles, are easier to do using the WP_User. Others are more straightforward with the WC_Customer.

This is what we need to do to change the user role:

Next we need to add the user’s billing and shipping details.

 

How to search programmatically created WooCommerce Customers

At this point we have customers, customer data, WooCommerce orders and other customer specific data in your WooCommerce store. Then it makes sense to filter them and better understand our customer base.

You can use Users Insights to do that. For example, we can filter all customers who were created using our code and who bought the Gold Membership product:

Filter users created programatically and who have bought a specific product

 

In addition, you can filter users by additional WooCommerce order criteria. For example, we can find all repeat buyers with lifetime value greater than 200.

Get lifetime value for repeat buyers

Conclusion

Today we looked into how to programmatically create WooCommerce Customers. In addition, we explored many ways to assign your user data, custom field and changing WooCommerce custom roles. Then we saw how you can use Users Insights to filter your customers and better understand them.

The code snippets here can be used in many cases, such as migrations, API integrations, guest checkout migration.

We hope you enjoyed, and see you next time!