WC_Customer class explained

Today we look into the WC_Customer class, and how you can use many of its methods. This article is a quick reference guide, with examples and details of the most common methods.

The WC_Customer class is an interesting way to manipulate your customers data. At its core, WooCommerce customers are just WordPress users. But since WooCommerce has so many functions enabled for order-related data, it makes sense to use the WC_Customer class.

Thus, this article is a quick reference to the most common methods in the WC_Customer class. You can use it to quickly retrieve order data, customer details, set or update user data. In addition, you can combine it with other WooCommerce and WordPress functions to get all your customer data.

Let’s get started!

wc_customer

WC_Customer class overview

The WC_Customer class can be used to get details of WordPress users who bought items in your WooCommerce store. Its basic usage is to initialize the class passing the user ID:

This code returns a customer object for that particular user ID. If no ID is set, you will still get an object partially filled in with the current query.

Creating a customer programmatically

Now let’s explore two common ways to create a WooCommerce customer.

The first method uses the wc_create_new_customer() function and allows you to pass custom user arguments – it supports the same arguments that you can pass to the List of arguments to pass to wp_insert_user() function.

The second method works the same way, it creates a new user with the ‘customer’ role. It attempts to load some of the customer data from the current query, but you can manually set all arguments using the methods that we are going to explore.

 

Retrieve and update personal user information

Once you have the WC_Customer instance, you can get and update all the user information with its corresponding methods.

This function loads the login username for the customer. Like we saw before, you can use the set_username with a new WC_Customer class.

This method won’t work with existing users though, it works only before the WP user has been created.

Therefore, some of the set_{data} methods are reserved only for the customer creation. They can’t be used afterwards.

The get_email method retrieves the customer email. It works the same way as the WP function $data = get_userdata($id); followed by $data->user_email.

The set_email method has the same restriction as the set_username, it can only be used prior to the customer creation.

The following methods can be used to get or set the name fields for your customer. When using the set methods you need to use the save() method as well, so your data is pushed to the database.

The get_role method returns the customer’s role. The set_role method works only prior to the user creation. To assign a different role to your users after they are created you need to use the WordPress functions, such as set_role for a WP_User object.

The get_date_created method returns a WC_DateTime object with the customer’s creation date.

Customer orders methods

There are many methods to get order information directly from the WC Customer class. Let’s see how we can use a few of them now.

The get_order_count() method returns how many order this customer has:

This method gets a WC_Order object for the customer’s latest order:

You can use this method to get the lifetime value of a customer. Thus it returns you the total dollar amount spent, including taxes, fees, shipping:

You can get all orders for a specific customer by using the following code snippet:

This code isn’t a method for the WC_Customer class, but still an interesting snippet.  In this case, we are using the customer ID, but you can use other fields, such as email, first name, last name, billing or shipping address. You can read more about the wc_get_orders() function to see the query arguments available.

Billing and Shipping details

All billing and shipping information is saved as custom user fields. But you can interact with it using the WooCommerce methods, to make sure that you always get the right fields.

Each field has its own separate method, but they all work very similar. Here’s a quick list of all individual methods:

The exact same methods are available for shipping, except the email field:

And each of these methods have their corresponding set methods. Here is an example of how you would edit the billing email:

Another interesting method is to set the billing or shipping address to match the store’s address. It gets data from wc_get_customer_default_location() and saves it to your user. Usually this function returns just the state and country, but you can add more fields to it using hooks.

And here is a quick overview of all the “set” methods for both shipping and billing address in the WC_Customer class. Don’t forget to save your data after you use the set method:

Customer metadata

You can Interact with your user’s metadata as well. This is possible with these WC_Customer methods:

This code returns an array with multiple objects, each containing a WC_Meta_Data object for all custom user fields but the ones that are created with WooCommerce.

To add a custom user field you can use the add_meta_data method which also initializes a WC_Meta_Data object for it. The $unique variable is optional, and if it is set as true, all previous data for this meta key is deleted. It doesn’t save this data to the custom user field though. You can do it with the save_meta_data() method:

The delete_meta_data method removes all custom user fields entries with that particular key:

As we mentioned above, the save_meta_data() method saves all meta data changes into the custom user fields permanently. It is useful in case you are running multiple add/delete operations and you want to push all changes at once:

You can alternatively use the update_meta_data() method as a shorthand for add_meta_data and save_meta_data. This method pushes the meta data directly to your database. You can provide the meta field ID if you want to update a specific value (if not unique).

The read_meta_data method reads data from your database directly, ignoring all the non-saved meta data from the add_meta_data and delete_meta_data methods.

The set_meta_data() method is a shorthand notation for multiple add_meta_data instances. You can pass multiple arguments with key/value/id attributes.

How to filter customers by their metadata

In addition to manipulating your users, you can filter them by their orders, products, location. You can use Users Insights along with the smart filters for that. For example, you can filter all repeat buyers from Spain by using these filters:
Filter users by billing country and orders count

Conclusion

Today we looked into how you can initiate and edit your customer data using the WC_Customer class. In addition to simple operations we saw how you can get advanced information such as all orders and even how to filter users.

We hope you enjoyed, and see you again next time!