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

时间:2017-01-15 09:54:54

标签: 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_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' );
    }
}

我想向所有用户角色显示该结帐通知,但&#39;订阅者&#39;用户角色。我试图这样做没有成功。

我怎样才能做到这一点?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试从当前用户获取用户对象和当前角色。然后,您将在现有条件中使用它,即显示检查通知。

这是自定义代码:

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%这是否会起作用...