# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, payments, roles, b2b
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-gateway-for-roles/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Payment Methods Only for Certain Roles" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
