AaronDT
AaronDT

Reputation: 4060

How to hide a payment option in woocommerce if user is not logged in?

I am using a custom function in functions.php to hide a payment option in case a user belongs to a certain role. Now I would also like to hide the option, if the user is not signed in. I tried using the "is_user_logged_in" however this does not seem to work for me.

Here is my code in functions.php:

function invoice_manager( $available_gateways ) {


global $woocommerce;
if ( isset( $available_gateways['invoice'] ) && current_user_can('customer') || !is_user_logged_in()   ) {
unset( $available_gateways['invoice'] );
} 
r

eturn $available_gateways;
}


add_filter( 'woocommerce_available_payment_gateways', 'invoice_manager' );

Upvotes: 1

Views: 412

Answers (1)

jrswgtr
jrswgtr

Reputation: 2379

Change your if statement like this:

if ( isset( $available_gateways['invoice'] ) && ( current_user_can('customer') || !is_user_logged_in() )   ) {
    unset( $available_gateways['invoice'] );
}

Upvotes: 2

Related Questions