Are you trying to set user roles in WooCommerce automatically based on purchase products?

Assign different user roles based on purchasing products is a very essential feature for your online store. WordPress has it’s own user roles which defines the user what part they can access in your website. But for online store customers you can mange user in many ways.

Using Woocemmerce user management you can hide any part of your website for a certain customers or you can offer a discount to the users who already purchased from your website. And also for new customers you can create a special discount to make themselves buy from your store.

All this features will available for you if you can manage user roles properly and you can get a better audience engagement by this. A better audience engagement lead s you to a better sells. So it is very important to understand and manage the user roles of your customers.

Here in this website you will guide you to automatically set user roles in WooCommerce after purchasing any product.

How to register a new WooCommerce user role

Initially you need to create new user role to to manipulate user roles. If you have already created new user role then do not need to do that. To create a new user role you need to run a little code snippets. You can run this code in any of your website pages. After running the code your user role will be created to the database. Then you can remove the code. Here is the code snippets-

function uiwc_new_role() {  
  //add the special customer role
    "Special Customer",
      'read'         => true,
      'delete_posts' => false
add_action('admin_init', 'uiwc_new_role');

After creating the role you can assign your user to this role. You can also various capabilities to this role.

How to change user roles after purchase any products

There are many different criteria available after purchasing a product. When any user purchase a product we can see the billing information. We will make a code snippets which will search for the billing email and check if there is a user registered or not for that email adders. If it is registered as a user then the user role of that user will change automatically.

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

This code snippets will make sure to change the user roles after purchasing product. You need to add this code in your function.php file. After adding the code your customer user role will be change automatically after purchasing a product.

We hope this article will help you. If you like this article please like our Facebook page. If you have any problem you can check our Website Maintenance services.