How to change WooCommerce customer roles programmatically
This article will show you different methods to change WooCommerce customer roles programmatically. You’ll also understand when this feature can be helpful for your site and what you can do with custom user roles.
What are user roles in WooCommerce?
WooCommerce user roles are predefined sets of permissions and capabilities assigned to users on an online store powered by WooCommerce. These roles determine users’ actions and access levels, such as managing orders, products, or settings, providing a flexible way to delegate responsibilities and maintain security within the e-commerce platform.
WordPress user roles and WooCommerce user roles are related but serve distinct purposes. WordPress user roles control access and permissions across the entire WordPress site, including content creation and administrative tasks. In contrast, WooCommerce user roles specifically manage capabilities related to e-commerce functions within the WooCommerce plugin, such as handling orders, products, and settings, providing a more tailored control system for online store operations.
WordPress user roles and WooCommerce user roles are related but serve distinct purposes.
WordPress uses roles to control what different types of WordPress users can do on your website. For example, administrators can do pretty much anything on a WordPress site; however, subscribers can access their 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 so that only specific WooCommerce customers can access certain products. Or you can unlock offers only after a user has purchased a particular product and their user role has been changed to a different one. It’s also possible to control user access to specific categories even for membership-like websites, to have different prices based on user roles.
Once you can manage and change your user roles, you can provide excellent customization and features. Therefore, our goal for today is to look into two different ways you can set your users to different roles:
- Change WooCommerce user role on purchase
- Bulk changes WooCommerce user roles. We’ll learn different methods for filtering users into different roles.
Important: The code snippets below directly modify the WordPress user settings. We do not recommend running this code on your production site unless it has been thoroughly tested on your development/staging environment.
Registering a new WooCommerce user role
Before we can apply our changes to the different 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.
Ideally, 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:
function uiwc_new_role() { //add the special customer role add_role( 'special-customer', "Special Customer", array( 'read' => true, 'delete_posts' => false ) ); } add_action('admin_init', 'uiwc_new_role');
Let’s see what each part of our code does:
- add_role( $name, $display_name, $capabilities) creates a new role with the powers we set. Ensure 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 for 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 the woocommerce_thankyou hook again, so 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.
This time, our approach is very similar to another tutorial, where we teach how to create an account for guest users automatically. We’ll get our WooCommerce order information and look into the billing email inside of that information. Then, we’ll check if there’s a user registered for that email. If this is a user, then we’ll automatically their user role.
Let’s take a look at our code for that:
function uiwc_change_role( $order_id ) { // get all the order data $order = new WC_Order($order_id); //get the user email from the order $user = $order->get_user(); // if the this is a registered user and this user is not an admin if( false != $user && !user_can($user, 'administrator') ){ // our new role name $role = 'special-customer'; //set the new role to our customer $user->set_role($role); } } //add this newly created function to the thank you page add_action( 'woocommerce_thankyou', 'uiwc_change_role', 100, 1 );
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 (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 you can change the user role when they meet 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 a registered user has made the order. With the second part, we ensure this is not an admin user. Sometimes, admin users create orders for testing purposes, and we want to ensure we don’t change their roles, as this could lock them out from the admin. Similarly, you can check against other parts you don’t want to change.
- $user->set_role($role) – this line 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 ones. If you want 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 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 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 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.
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, you may use a different approach if you need to perform a more selective user change.
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 only want to apply the special customer role to repeat buyers. We’ll filter them all and add them to a special customer group:
You need to select them all and add them to your user group now:
It’s important to remember 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:
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:
function uiwc_set_custom_role() { //the slug of our newly created group $group_slug = 'repeat-buyers'; //the role we want to change our users to $user_role = 'special-customer'; //telling WP which taxonomy we're looking into $taxonomy = 'usin_group'; //getting the UI group information $term = get_term_by( 'slug', $group_slug, $taxonomy, 'ARRAY_A' ); // getting the users from that group $id = $term['term_id']; $out = get_objects_in_term( $id, $taxonomy ); //checking if there are any users if( ! empty( $out ) ) { //let's loop trhough all users foreach ( $out as $uid) { //get the user related to this ID $user = new WP_User($uid); //do not change the role of admins if( !user_can($user, 'administrator') ){ //set the new role to our customer $user->set_role($user_role); } } } } add_filter('admin_footer', 'uiwc_set_custom_role');
This code allows you a whole new level of control over how to change your WooCommerce customer roles. You can apply any filters you want and 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 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 how 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 ones.
- add_filter(‘admin_footer’, ‘uiwc_set_custom_role’); – if you need to disable this code quickly, comment or remove this line, you don’t need to remove the entire code
And that’s it! Save that code in your child theme’s functions.php file, load any wp-admin page, and you’ll see the alert telling you the code has run.
How to Create WooCommerce custom user roles
To create a custom user role in WooCommerce, you can use the free User Role Editor plugin. This plugin allows you to customize the default user roles and create new ones based on them. You can also use this plugin to create custom WooCommerce roles based on an existing user and edit the existing ones.
To create a new user role using the User Role Editor plugin, go to Users > User Role Editor in the WordPress admin panel. Here, you can see different capabilities listed; on the right side, you can see an Add Role button. You can easily create a new role using this plugin by clicking the Add Role button and entering the desired name for the new role.
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!