[英]Remove or hide “add new” button on woocommerce on bulk order panel
[英]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.