1
votes

In my WooCommerce webshop, I am using a plugin that displays a checkout notice with the following function:

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

I would like to display that checkout notice to all user roles, except 'subscriber' user role. I have tried to do it without success.

How can I achieve this?

Thanks.

1

1 Answers

1
votes

You could try to get the user object and the current roles from this current user. Then you will use this in the existing condition, that is displaying the check notice.

Here is that customize code:

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

As I can't test it really, I am not sure at 100% that this is going to work…