【问题标题】:Bulk add new role to list of WooCommerce users将新角色批量添加到 WooCommerce 用户列表
【发布时间】:2019-01-17 00:12:06
【问题描述】:

我有一个从我们的 WooCommerce 网站购买产品的 141 位客户的列表。我使用 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 用户列表的更好方法?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您的代码中有两个问题

      这一行:

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

      我只是测试了代码,它正在工作

      【讨论】:

      • 非常感谢您的回答...这绝对有帮助。起初,它不起作用,但后来我重新添加了旧的 $user_role,因为这实际上是它的 slug: $user_role = 'homeschool-strong';这很有效!
      • 不客气,如果你喜欢我的回答,请接受:)
      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 2018-09-25
      • 2021-06-01
      • 2017-10-03
      • 2022-11-27
      • 2018-09-10
      • 1970-01-01
      相关资源
      最近更新 更多