简体   繁体   中英

Bulk add new role to list of WooCommerce users

I have a list of 141 customers that purchased a product from our WooCommerce site. I pulled the list using the UserInsights plugin ( https://usersinsights.com/ ).

What I am trying to do is to bulk add a different role to these users on top of whatever they have and if they already have the new role to simply not do anything to that specific user.

Using this article from UserInsights ( https://usersinsights.com/woocommerce-change-customer-role/ ), I tried adding the following code to my functions.php, it shows that the code is executed, but the users never receive the new role.

Here is the code I added to my functions.php:

function uiwc_set_custom_role() {
  //the slug of our newly created group
  $group_slug = 'homeschool-strong';

  //the role we want to change our users to
  $user_role = 'homeschool-strong';

  //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->$user->add_role($role);
  }

}

// just so you know the code worked
echo "<script>alert('code run');</script>";
}
 }

add_filter('admin_footer', 'uiwc_set_custom_role');

Would anyone know why when this code is executed the new role is not added to the users? Or maybe you know of a better way to add a new role to a list of WooCommerce users?

you have two issue in your code

this line:

$user->$user->add_role($role);

should be as follow:

$user->add_role($user_role);

explanation you are using the object twice and you didn't defined the variable role you have declared as user_role

second issue:

$user_role = 'homeschool-strong';

in your declaration WordPress will treated as array not as string so it should be like this :

$user_role = 'homeschool_strong';

so the full code should be like this:

function uiwc_set_custom_role()
{
    //the slug of our newly created group
    $group_slug = 'homeschool-strong';

    //the role we want to change our users to
    $user_role = 'homeschool_strong';

    //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->add_role($user_role);
            }

        }

// just so you know the code worked
        echo "<script>alert('code run');</script>";
    }
}

add_filter('admin_footer', 'uiwc_set_custom_role');

i just test the code and it's working

With the help of kashalo the final code that worked was:

function uiwc_set_custom_role()
 {
//the slug of our newly created group
$group_slug = 'homeschool-strong';

//the role we want to change our users to
$user_role = 'homeschool-strong';

//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->add_role($user_role);
        }

    }

// just so you know the code worked
    echo "<script>alert('code run');</script>";
   }
}

add_filter('admin_footer', 'uiwc_set_custom_role');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM