避免向特定用户角色显示结帐通知

Avoid displaying a checkout notice to a specific user role

在我的 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_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' );
    }
}

我想向所有用户角色显示结帐通知,'subscriber' 用户角色除外。我试过没有成功。

我怎样才能做到这一点?

谢谢。

您可以尝试从该当前用户获取用户对象和当前角色。那么你将在现有的情况下使用它,即显示支票通知。

这是自定义代码:

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

因为我无法真正测试它,所以我不能 100% 确定这是否会起作用……