How to change WooCommerce customer roles programmatically

In this article we are going to show you different methods to change WooCommerce customer roles programmatically. You’ll also understand when this feature can be useful for your site, and what you can do with custom user roles.

WordPress uses roles are a way of controlling what different types of WordPress users can do on your website. For example, administrators can do pretty much anything in a WordPress site, on the other hand, subscribers have access to their own profile data only. If you’ve got an online store, user role management can be used in many ways. You can set up product visibility based on user role in a way that only certain WooCommerce customers can have access to certain products. Or you can unlock offers only after a user has purchased a certain product and their user role has been changed to a different one. It’s also possible to control user access to certain categories even, for membership like websites, to have different prices based on user roles.

Once you are able to manage and change your user roles, you can provide a great level of customization and features. Therefore, our goal for today is looking into two different ways you can set your users to a different role:

  1. Change WooCommerce user role on purchase
  2. Bulk change WooCommerce user role – we’ll learn different methods for filtering users into different roles

Important: The code snippets below apply direct modifications to the WordPress user settings. We do not recommend running this code on your production site unless thoroughly tested on your development/staging environment.

change woocommerce customer role

Registering a new WooCommerce user role

Before we can start applying our changes we to the different our user roles we need to create a new user role. If you already have the custom role created, feel free to skip to the next step.

In an ideal scenario you would register your role when a plugin or theme is activated. In our case we can run it into the init hook.

Here is the code to do it:

Let’s see what each part of our code does:

  • add_role( $name, $display_name, $capabilities) – this function creates a new role with the capabilities we set. Make sure that you set a unique role name that doesn’t exist yet. You can also set all the capabilities that you need in this section.
  • add_action( 'admin_init', 'uiwc_new_role') – sets the code to run in the admin. This is where you can change to a plugin or theme activation hook for better results.

You can run this code by visiting any of your admin dashboard pages. Once you run this code, feel free to remove it, as the role will be created in the database and this code won’t be needed to run anymore.

We can now assign some users to our newly created role. You can also use different WooCommerce capabilities lists in your new role. You can set a whole new access control to your users since that is created from scratch.

 

Change WooCommerce user role after purchase

There are many different hooks we could use for changing the user role. We’ve chosen once again the woocommerce_thankyou hook, so that means that we’ll run our code when the user sees the thank you page. This means that we are changing the user role right after the purchase.

Our approach this time is very similar to another tutorial, where we teach how to automatically create an account for guest users. We’ll get our WooCommerce order information, and inside of that information we’ll look into the billing email. Then we’ll check if there’s a user registered for that email. If there is a user, then we’ll automatically change their user role.

Let’s take a look at our code for that:

You can see that this code has some measures to make sure that the WooCommerce user role is changed only after purchase. We’ve set them to the special customer role, but you can set your customers to any other role you want to (including predefined roles, such as editor).

The important aspects of that code are:

  • $order = new WC_Order($order_id)- here we get all the current order data. This is great, because it means that you can change the user role just when they meet some criteria like buying a certain product or a product from a certain category
  • $user = $order->get_user(); – get the user for this order. This function will return false if this is a guest user.
  • if( false != $user && !user_can($user, 'administrator') )  – the first part checks whether the order has been made by a registered user. With the second part we make sure that this is not an admin user. Sometimes admin users create orders for testing purposes and we want to make sure that we don’t change their roles, as this could lock them out from the admin. In a similar manner you can check against other roles that you don’t want to be changed.
  • $user->set_role($role) – this is the line that sets our current user object to have a new role. Please keep in mind that this code replaces the existing user roles with the new one. If you would like to keep the previously assigned roles and only add the new role, use   $user->add_role($role) instead.

You can add this code to the functions.php file of a child theme or to a custom plugin. Once you do that, the WooCommerce customer role will be changed for each user after purchase.

However this change will only work for the future orders. Sometimes you may need a way to bulk change past users. Let’s see how to proceed in this case.

 

Bulk change WooCommerce user roles

Although changing the user role based on certain conditions is great, maybe you need to change them in bulk. If you just want to change the default role for all your past customers, it’s an easy fix – you can use a plugin such as the Bulk Role Change to update all users.

WooCommerce change user role in bulk

However, this plugin doesn’t have many options, and you can only change all WordPress users from a certain role to another, with no exceptions. Hence, if you need to perform a more selective user change you may use a different approach.

Changing user roles based on different conditions

You could use Users Insights in combination with a bit of custom code to change all users from a certain group to another role. As a result, you can use all the filtering capabilities contained in Users Insights to change just the users you want to.

The first step of this method is filtering your users. Let’s say you want to apply the special customer role only to repeat buyers. We’ll filter them all and add them to a special customer group:

woocommerce change customer role custom

You just need to select them all, and add them to your user group now:

Adding WooCommerce repeat buyers to user group

It’s important to keep in mind that you can change your filtering criteria to be as complex or as easy as you want. You can also save this search for later use using the Segments option. In this way you can change your WooCommerce customer roles at all times:

Repeat buyers WooCommerce segment

After that we need a bit of custom code that will run through your user list, find the ones with that group and modify them the way we want. In this case this is the code to do it:

This code allows you a whole new level of control of how to change your WooCommerce customer roles. You can apply any filters you want, add them to a group and then this code will do the heavy lifting.

Let’s see what each part of this code does:

  • $group_slug = 'repeat-buyers'; – replace this part with your own group slug
  • $user_role = 'special-customer'; – replace this with your own customer role
  • $term = get_term_by( 'slug', $group_slug, $taxonomy, 'ARRAY_A' ); – this is the way we get the details about that user group taxonomy, otherwise you’d need to get it by its ID (which is not as easy as using the slug)
  • $out = get_objects_in_term( $id, $taxonomy ); – at this point we have all the user IDs who are tagged in that group. After this it’s just a matter of looping through them and changing their role as we’ve done before
  • if( !user_can($user, 'administrator') )  – make sure that this user is not an admin user, as changing their role could lock them out from the admin dashboard
  • $user->set_role($user_role); – changes the customer role to the new role. Once again, you can use   $user->add_role($role)  if you want to keep the current user roles (instead of replacing them) and only add the new one.
  • add_filter('admin_footer', 'uiwc_set_custom_role'); – if you need to quickly disable this code, just comment or remove this line, you don’t need to remove the entire code

And that’s it! Just save that code in your child theme’s functions.php file, load any wp-admin page and you’ll see the alert telling you that the code has run.

Conclusion

Today we looked into how you can change your WooCommerce customer roles using custom code and Users Insights. We looked into different approaches, each with its own benefits. By the end of the day you should be able to create your own user roles and apply them to the users you want to.

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