Change Woocommerce default user Role

Easy way to Change Woocommerce default user Role.

Hello buddy,

I want to share something with you about Easy way to Change Woocommerce default user Role customer to something you need or like. I was working on LMS project so wrote down few lines of code that I want to share with you. May it help you.

Woocommerce allows WordPress user registration at checkout and my account page. It creates user by default role as customer. But for purpose of your project,sometime you may need other role other than customer. As in LMS project, I wanted user to have student role, who registers at checkout page. So not need to worry for that. Woocommerce Gives a control over it using via a filter `woocommerce_new_customer_data`.

In \wp-content\plugins\woocommerce\includes\wc-customer-functions.php : line 102

<?php
$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
    'user_login' => $username,
    'user_pass'  => $password,
    'user_email' => $email,
    'role'       => 'customer'
) ); ?>

In above lines extracted from Woocommerce, woocommerce_new_customer_data is filter applied over $new_customer_data variable. Still user is not registered, so you can deal with users data as you wish, wp user get created.

You can filter User Role by changing value in Array ($new_customer_data) provided by woocommerce_new_customer_data filter. You must use add_filter action in you theme’s function.php file or your plugin file. Write your own function like my_new_customer_data().

Dafault format of filter is as below.

<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?> 

You have to pass minimum 2 parameters to add_filter action. i.e. $tag, $function_to_add

<?php add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data'); ?>

In your function just have to replace value of key index which is ‘role’. Change WooCommerce default user Role  You can directly specify user role like option one as shown below or You can get default user role from WordPress default user role option(Which is in general settings) like option two.

Option one:

<?php $new_customer_data['role'] = 'student'; ?>

Option two:

<?php $new_customer_data['role'] = get_option( 'default_role' ); ?>

Hence finally you just have to add below lines of code to your theme or plugin. Enjoy WordPress and Woocommerce with your project. Happy coding. 🙂  🙂  🙂

<?php
function my_new_customer_data($new_customer_data){
 $new_customer_data['role'] = get_option( 'default_role' );
 return $new_customer_data;
}
add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data');
?>

26 thoughts on “Easy way to Change Woocommerce default user Role.”

  1. Hello Makarand Mane, I know that this post is very old and I’m sorry to reopen this discussion. I’ve been looking for a solution to my case and I found this post and I’m pretty sure that I can adjust the solution provided here for other cases to mine, although I’m struggling to do it.

    I need to change the user role after purchase based on an information he set in a custom field. My store is from Brazil and here we need a plugin that adds some new fields. One of them is called “billing_persontype” and basically it only has two options to be defined with the value 1 or 2.

    I’ve seen this part of your code:

    add_action( ‘woocommerce_thankyou’, ‘change_user_role_to_customer’ );
    function change_user_role_to_customer( $order_id ) {

    $order = new WC_Order( $order_id );
    $user_id = $order->user_id;
    $user_info = get_userdata($user_id);

    if(!in_array(get_option( ‘default_role’ ), $user_info->roles))
    return;

    wp_update_user( array( ‘ID’ => $user_id, ‘role’ => ‘customer’) );

    }

    And I’m wondering if I can adjust the parameters to get the value from this field (billing_persontype) and if it’s 2 (for exemple) change the user role to a specific one, otherwise do nothing.

    Is it possible to make few changes in your code to reach that?

    Any help will be fully appreciated! Thanks

  2. Hi, Great code, helps a lot. However, i am looking to check the customer email address and assign them to a custom role based on their email. For example, if the email address is [email protected], i want to assign them to a new role called “yahoo customers”
    thanks

    1. Use strpos function and check for string “yahoo”

      if (strpos($email, 'yahoo') !== false) {
          $new_customer_data['role'] = 'yahoo_customer';
      } 

      Hope this helps!! 🙂

      1. Forgive me for being a novice but i added this:

        function my_new_customer_data($new_customer_data){
         //$new_customer_data['role'] = get_option( 'default_role' );
         if (strpos($email, 'yahoo') !== false) {
            $new_customer_data['role'] = 'student_customer';
        } 
         return $new_customer_data;
        }
        add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data');

        to my functions.php but its still coming up as the default role of ‘customer’

        1. In my reply I mean that, check string inside email address. In your function $email is not defined. Just make little change $email to $new_customer_data[‘user_email’].

          function my_new_customer_data($new_customer_data){
           //$new_customer_data['role'] = get_option( 'default_role' );
           if (strpos($new_customer_data['user_email'], 'yahoo') !== false) {
              $new_customer_data['role'] = 'student_customer';
          } 
           return $new_customer_data;
          }
          add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data');
  3. Hi Buddy,

    You do nice work! And you are generous giving out code. I wonder if you could point me to changing roles between 2 different kinds of subscribers in WooCommerce. WC has a setting for default role for subscribers (active and expired), but if I have a pro and an amateur buying different subscriptions, I don’t want them to have the same role (active and expired). I use roles to manage what people see in the menus and I don’t want amateurs to see pro links. How would I do this? Thanks!

    1. Hi Beatty,

      Its simple by the way. Just create 2 different menus. Depending on role show that menu.

      $user_info = get_userdata( get_current_user_id() );
      
      if( !in_array( 'pro_user' ,  $user_info->roles ) )
         echo $promenu;
      else
         echo $basic_manu;
      
  4. Hi sorry I don’t understand.

    As I said : yes it’s works with last version 2.5.2 of Woocommerce.

    But with your script we can modify the role of the new users : it’s ok ! But on the other hand if they buy a product they do not become “customer ” they always keep the same role which is define in your script ?

    Do you know if it is possible to have a role for the new users as with your script but who become “customers” when they buy on the shop?

    Thanks for your expertise

    1. Hi

      This filter is called when a new user is created by woocommerce. So assigned role will be forever. Woocommerce is not going to change user’s role to customer when they buy product. You can change it, at some situation also using any other filter or action.

      If you tell me what you trying to do then I will suggest best possible things which you should do.

      1. Hi,

        thanks for your help and expertise

        – On my shop I’ve people who create account but they don’t buy product, I would like that their role is : “subscriber”
        – But If this people buy something, I would like that their role is : “customer”

        Do you know if it’s possible ?

        thanks

        1. Yes it is possible. Use same code above to set user’s role to subscriber when he sign up. After completion of order (I mean order complete with payment) you have to change user’s role.

          Woocommerce template have thank page template and on this template it calls ‘woocommerce_thankyou’ action. Just change user’s role at this point.

          add_action( 'woocommerce_thankyou', 'change_user_role_to_customer' );
          
          function change_user_role_to_customer( $order_id ) {
          
          $order = new WC_Order( $order_id );
          $user_id = $order->user_id;
          
          wp_update_user( array( 'ID' => $user_id, 'role' => 'customer') );
          
          }
          1. Hi Marakand Mane,

            your script is correct. There is just a little little problem… 🙂

            If I make a purchase with my administrator’s account to make test, I become a customer too…
            Well, after that I can’t access to administration… oups !

            Do you know if I can exclude some role like administrator or editor with your script… Thanks for this good script !

          2. If you want to change role of user who are subscriber OR your wordpress default role then first compare role of user you get from order.

            $user_info = get_userdata($user_id);
            if(!in_array(get_option( 'default_role' ), $user_info->roles))
            return;

            If we did not get default role for user of order then we will exit that function. So your final code will be like this.

            add_action( 'woocommerce_thankyou', 'change_user_role_to_customer' );
            function change_user_role_to_customer( $order_id ) {
            
             $order = new WC_Order( $order_id );
             $user_id = $order->user_id;
             $user_info = get_userdata($user_id);
            
             if(!in_array(get_option( 'default_role' ), $user_info->roles))
              return;
            
             wp_update_user( array( 'ID' => $user_id, 'role' => 'customer') );
            
            }
  5. Hi,

    I made out a will and your script works all the same. 🙂

    With your script we can modify the role of the new users, on the other hand if they buy a product they do not become “customer ” they always keep the same role.

    Do you know if it is possible to have a role for the new users as with your script but who become “customers” when they buy on the shop?

    Thanks for your expertise

    1. Array of $new_customer_data is filtered beofre adding new customer as user in wordpress. So everytime a user fills woocommerce sign up form he wil be assigned role we are going via this filter.

      1. Hi,

        yes it works
        With your script we can modify the role of the new users, on the other hand if they buy a product they do not become “customer ” they always keep the same role.

        Do you know if it is possible to have a role for the new users as with your script but who become “customers” when they buy on the shop?

        thanks for your expertise

        1. What actually you want to do? You want to assign role to user other than customer or WHAT?

          If you add this filter then new user coming via woocommerce signup form will get role, you are passing from filter. If at some condition you want to give original role then you should remove that filter.

          remove_filter( 'woocommerce_new_customer_data', 'my_new_customer_data');
          1. Hi,

            thanks for your help.
            Sorry If I’m not clear :S

            – On my shop I’ve people who create account but they don’t buy product, I would like that their role is : “subscriber”
            – But If this people buy something, I would like that their role is : “customer”

            With your code users are always “subscriber” even when they buy product.

            Do you know if it’s possible ?

    1. About which file you are talking about.

      Is that this one “\wp-content\plugins\woocommerce\includes\wc-customer-functions.php” then YES it is. You dont need to change anything in woocommerce. You have to place last one code in functions.php file of your theme.

      1. Hi Makarand Mane,
        Thanks for your answer.

        Sorry, yes I’m talking about “\wp-content\plugins\woocommerce\includes\wc-customer-functions.php” but this file doesn’t exist in last version of Woocommerce.
        Well your good script can’t work ?

        I’ve download another time woocommerce, to make sur about that, and as you can see, there is no “\wp-content\plugins\woocommerce\includes\wc-customer-functions.php” : https://www.woothemes.com/woocommerce/

        Thanks 🙂

        1. Which woocommerce version you are using?

          If it latest then check this file. `/includes/wc-user-functions.php` line number 102
          For more details you can check http://hookr.io/filters/woocommerce_new_customer_data/

          wc-user-functions.php file location in latest woocommerce 2.5.2

          But final code is same and working for latest woocommerce also. Please check and reply. Also check if any other filter is applied. Then you can increase priority of filter to apply it at end.

          add_filter( 'woocommerce_new_customer_data', 'my_new_customer_data', 100);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top