Reverse this function to make it exclusive to 1 user role

I have been hunting around for a solution to this and can’t seem to find it. I’m wanting to make a payment gateway available exclusively to one user role, but I’ve only been able to find this code. Can anyone help me with an idea for how to write something to only allow this for only 1 of my 4 different user roles in addition to keeping it from non-logged in users?

function wdm_disable_woocommerce_gateway_purchase_order( $available_gateways ) {

//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if ( isset($available_gateways['woocommerce_gateway_purchase_order']) && (current_user_can('customer') || ! is_user_logged_in()) ) {

    //remove the cash on delivery payment gateway from the available gateways.

     unset($available_gateways['woocommerce_gateway_purchase_order']);
 }
 return $available_gateways;
}

add_filter('woocommerce_available_payment_gateways', 'wdm_disable_woocommerce_gateway_purchase_order', 99, 1);

Related posts

1 comment

  1. If you want to only allow logged in users who have a particular user role, simply change the logic in the snippet you posted:

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

    This would remove the gateway for users who:

    1. are not logged in.
    2. are logged in, but not a customer.

    Just change customer to your preferred user role or capability.

Comments are closed.