Php 避免向特定用户角色显示签出通知
在我的WooCommerce网店中,我使用了一个插件,该插件显示一个具有以下功能的结帐通知:Php 避免向特定用户角色显示签出通知,php,wordpress,woocommerce,checkout,user-roles,Php,Wordpress,Woocommerce,Checkout,User Roles,在我的WooCommerce网店中,我使用了一个插件,该插件显示一个具有以下功能的结帐通知: public function action_add_checkout_notice() { $balance = (float) get_user_meta( get_current_user_id(), 'affwp_wc_credit_balance', true ); $cart_coupons = WC()->cart->get_applied_c
public function action_add_checkout_notice() {
$balance = (float) get_user_meta( get_current_user_id(), 'affwp_wc_credit_balance', true );
$cart_coupons = WC()->cart->get_applied_coupons();
$coupon_applied = $this->check_for_coupon( $cart_coupons );
$notice_subject = __( 'You have an account balance of', 'affiliatewp-store-credit' );
$notice_query = __( 'Would you like to use it now', 'affiliatewp-store-credit' );
$notice_action = __( 'Apply', 'affiliatewp-store-credit' );
// If the user has a credit balance,
// and has not already generated and applied a coupon code
if( $balance && ! $coupon_applied ) {
wc_print_notice(
sprintf( __( '%1$s <strong>%2$s</strong>. %3$s <a href="%4$s" class="button">%5$s</a>' ) ,
$notice_subject,
wc_price( $balance ),
$notice_query,
add_query_arg( 'affwp_wc_apply_credit', 'true', WC()->cart->get_checkout_url() ),
$notice_action
),
'notice' );
}
}
public function action\u add\u checkout\u notice(){
$balance=(float)get_user_meta(get_current_user_id(),'affwp_wc_credit_balance',true);
$cart_-coups=WC()->cart->get_-applicated_-coups();
$touch\u applicated=$this->check\u for\u优惠券($cart\u优惠券);
$notice_subject=uuuuu('You have account balance of','affiliatewp store credit');
$notice\u query=\uuuuuuu('you'want to use now','affiliatewp store credit');
$notice_action=uuuu('Apply','affiliatewp store credit');
//如果用户有贷方余额,
//尚未生成并应用优惠券代码
如果($balance&&!$coupon_适用){
wc_打印通知(
sprintf(“%1$s%2$s%3$s”),
$notice_主题,
wc_价格(余额),
$notice\u查询,
添加查询参数('affwp\u wc\u apply\u credit','true',wc()->cart->get\u checkout\u url()),
$notice\u行动
),
“通知”);
}
}
我想向所有用户角色显示该签出通知,但“订户”用户角色除外。我试着去做,但没有成功
我怎样才能做到这一点
谢谢。您可以尝试从当前用户获取用户对象和当前角色。然后您将在现有条件下使用它,即显示检查通知 以下是自定义代码:
public function action_add_checkout_notice() {
$balance = (float) get_user_meta( get_current_user_id(), 'affwp_wc_credit_balance', true );
$cart_coupons = WC()->cart->get_applied_coupons();
$coupon_applied = $this->check_for_coupon( $cart_coupons );
$notice_subject = __( 'You have an account balance of', 'affiliatewp-store-credit' );
$notice_query = __( 'Would you like to use it now', 'affiliatewp-store-credit' );
$notice_action = __( 'Apply', 'affiliatewp-store-credit' );
## ## Getting The User object and role ## ##
$user = wp_get_current_user();
$user_roles = $user->roles;
// If the user has a credit balance,
// and has not already generated and applied a coupon code
## (+) if user role isn’t 'subscriber' ##
if( $balance && ! $coupon_applied && !in_array('subscriber', $user_roles)) {
wc_print_notice(
sprintf( __( '%1$s <strong>%2$s</strong>. %3$s <a href="%4$s" class="button">%5$s</a>' ) ,
$notice_subject,
wc_price( $balance ),
$notice_query,
add_query_arg( 'affwp_wc_apply_credit', 'true', WC()->cart->get_checkout_url() ),
$notice_action
),
'notice' );
}
}
public function action\u add\u checkout\u notice(){
$balance=(float)get_user_meta(get_current_user_id(),'affwp_wc_credit_balance',true);
$cart_-coups=WC()->cart->get_-applicated_-coups();
$touch\u applicated=$this->check\u for\u优惠券($cart\u优惠券);
$notice_subject=uuuuu('You have account balance of','affiliatewp store credit');
$notice\u query=\uuuuuuu('you'want to use now','affiliatewp store credit');
$notice_action=uuuu('Apply','affiliatewp store credit');
####获取用户对象和角色####
$user=wp_get_current_user();
$user\u roles=$user->roles;
//如果用户有贷方余额,
//尚未生成并应用优惠券代码
##(+)如果用户角色不是“订户”##
如果($balance&&!$coupon_应用于($subscriber',$user_角色)数组中(&&){
wc_打印通知(
sprintf(“%1$s%2$s%3$s”),
$notice_主题,
wc_价格(余额),
$notice\u查询,
添加查询参数('affwp\u wc\u apply\u credit','true',wc()->cart->get\u checkout\u url()),
$notice\u行动
),
“通知”);
}
}
由于我不能真正测试它,我不能100%确定它是否能工作…太棒了!它工作完美!非常感谢LoicTheAztec!