簡體   English   中英

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

[英]Bulk add new role to list of WooCommerce users

我有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用戶列表添加新角色的更好方法?

您的代碼中有兩個問題

這行:

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

我只是測試代碼,它正在工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM