Payment Methods Only for Certain Roles
Shows chosen payment methods, such as "Pay by invoice" or cheque, only to trusted roles like trade customers, and hides them from everyone else.
Opens WPIDE on
Code
$wpide_role_gateways = array( // Gateway ID => roles that may use it.
'cheque' => array( 'wholesale_customer' ),
'cod' => array( 'wholesale_customer', 'customer' ),
);
add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) use ( $wpide_role_gateways ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return $gateways;
}
$roles = is_user_logged_in() ? (array) wp_get_current_user()->roles : array();
foreach ( $wpide_role_gateways as $id => $allowed ) {
if ( isset( $gateways[ $id ] ) && ! array_intersect( $allowed, $roles ) ) {
unset( $gateways[ $id ] );
}
}
return $gateways;
} );
Notes
Use gateway IDs from WooCommerce > Settings > Payments (bacs, cheque, cod); guests never see these methods.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.