根据 WooCommerce 中的注册电子邮件设置自定义用户角色 [英] Set a custom user role based on registration email in WooCommerce
本文介绍了根据 WooCommerce 中的注册电子邮件设置自定义用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的functions.php 文件中添加了一个自定义过滤器.当我第一次加载它时,它是几个版本的 Woocommerce 前,它运行得很好.
I added a custom filter to my functions.php file. When I first loaded it up which was a few versions of Woocommerce ago it worked perfectly.
现在似乎所有用户都注册为默认的客户"而不是学生",因为他们的电子邮件确实包含 .ac.uk
或 .sch.uk
>
Now it seems that all users are registered as the default 'customer' as opposed to 'student' when their emails do contain .ac.uk
or .sch.uk
过滤功能如下:
add_filter( 'woocommerce_new_customer_data', 'student_role', 10, 1 );
function student_role( $new_cust_data ) {
if ( strpos($new_cust_data['user_email'], '.ac.uk' ) >= 1 ) {
$new_cust_data['role'] = 'student';
}
elseif ( strpos($new_cust_data['user_email'], '.sch.uk') >= 1 ) {
$new_cust_data['role'] = 'student';
}
else {
$new_cust_data['role'] = 'customer';
}
return $new_cust_data;
}
有什么帮助吗?
推荐答案
尝试将代码中的 >= 1
替换为 !== false
(也可以简化)如下:
Try to replace >= 1
with !== false
in your code (also it can be simplified) as follows:
add_filter( 'woocommerce_new_customer_data', 'set_new_customer_user_role_conditionaly' );
function set_new_customer_user_role_conditionaly( $args ) {
if ( strpos($args['user_email'], '.ac.uk') !== false || strpos($args['user_email'], '.sch.uk') !== false ){
$args['role'] = 'student';
}
return $args;
}
它应该可以工作.
这篇关于根据 WooCommerce 中的注册电子邮件设置自定义用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文