【发布时间】: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