批量添加新角色到WooCommerce用户列表

时间:2018-08-09 13:36:52

标签: php wordpress woocommerce

我列出了141个从我们的WooCommerce网站购买产品的客户。我使用UserInsights插件(https://usersinsights.com/)拉出了该列表。

我想做的就是在这些用户拥有的一切之上,如果他们已经具有新的角色,则根本不给该特定用户做任何事情,从而为这些用户批量添加其他角色。

使用UserInsights(https://usersinsights.com/woocommerce-change-customer-role/)的这篇文章,我尝试将以下代码添加到我的functions.php中,它表明代码已执行,但用户从未获得新角色。

这是我添加到我的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');

有人知道为什么在执行此代码时不会将新角色添加到用户吗?或者,也许您知道将新角色添加到WooCommerce用户列表中的更好方法?

2 个答案:

答案 0 :(得分:0)

您的代码中有两个问题

此行:

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

应如下所示:

$user->add_role($user_role);

说明您两次使用该对象,并且未定义声明为user_role的变量角色

第二期:

$user_role = 'homeschool-strong';

在您的声明中WordPress将被视为数组而不是字符串,因此应如下所示:

$user_role = 'homeschool_strong';

因此完整的代码应如下所示:

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');

我只是测试代码,并且可以正常工作

答案 1 :(得分:0)

在kashalo的帮助下,有效的最终代码是:

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');
相关问题