tadrik Free store check

Guides / WooCommerce

Show payment methods by user role in WooCommerce

You sell to retail and wholesale customers from one store. Wholesale buyers should pay by bank transfer or on invoice, retail customers by card, and guests should never see cash on delivery. WooCommerce lets you switch a payment gateway on or off, but not by who is buying. Here are two ways to do it.

What WooCommerce does itself

Under WooCommerce > Settings > Payments every gateway is simply on or off for everyone. Cash on delivery can be limited to certain shipping methods, but no core gateway has a customer role or logged-in setting. Wholesale plugins sometimes add one for their own price lists; if you do not use one, you need a snippet or a rules plugin.

Option 1: a code snippet

Add this to a small custom plugin or your child theme's functions.php. It offers bank transfer only to users with the wholesale_customer role and removes cash on delivery for guests.

add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) {
    if ( is_admin() ) {
        return $gateways;
    }
    $roles = array();
    if ( is_user_logged_in() ) {
        $user  = wp_get_current_user();
        $roles = (array) $user->roles;
    }
    // Bank transfer (bacs) only for wholesale customers.
    if ( ! in_array( 'wholesale_customer', $roles, true ) ) {
        unset( $gateways['bacs'] );
    }
    // No cash on delivery for guests.
    if ( ! is_user_logged_in() ) {
        unset( $gateways['cod'] );
    }
    return $gateways;
} );

Notes on the snippet:

  • Role names are slugs, not labels. Look at the Users screen or your wholesale plugin's documentation for the exact one: it may be wholesale_customer, b2b or something else.
  • The gateway id is bacs for bank transfer, cod for cash on delivery and cheque for cheque. Other gateways use their own ids.
  • A user can hold several roles, so the snippet checks the whole list.
  • If you remove every gateway for some customer, they cannot pay. Check each role you care about before you leave it on.
  • Keep a copy of the file before you edit it: a mistake in a snippet can break the site.

Option 2: a rule in Tadrik Checkout Rules

Tadrik Checkout Rules is a free plugin on WordPress.org. Under WooCommerce > Checkout Rules, add a rule with the action "Hide payment methods", pick the gateway, and add the condition "Customer role". Guests count as a role of their own.

Hide Cash on delivery when the customer role is Guest.

To offer a method to one group only, hide it when the customer role is not that role. You can add more conditions to the same rule, such as country, cart total or shipping method.

Two safety points. If a rule would leave a customer with no way to pay, the plugin shows every method instead and warns you on the rules page. And the Test a cart tab lets you pick a customer, an address and some products, then shows which payment methods that customer is offered and which rule hid the rest, without placing an order.

Which should you pick?

  • Snippet: one or two fixed behaviours, and you are comfortable with code.
  • Plugin: several roles or rules, other conditions mixed in, or you want to test a cart as a given customer and switch a rule off without touching code.

Test it before customers do

Check the checkout three ways: logged out, logged in as a normal customer, and logged in as the special role. Use a test account for each rather than your admin account, which usually sees something different. Do it on a phone too.

Common questions

Can WooCommerce show a payment method only to some user roles? Not by itself. WooCommerce has no setting that ties a gateway to a customer role. You need a snippet or a plugin.

How do I treat guests differently from logged-in customers? In a snippet, check is_user_logged_in(). In Checkout Rules, the customer role condition includes guests, so you can hide a method for guests or show it only to them.

Does a user with two roles match either one? In the snippet below, yes: it matches if any of the user's roles is on your list. In Checkout Rules, the condition matches when any listed role applies to the customer.

Does it work with the block checkout? Checkout Rules applies its rules to the block checkout and the classic one. For a snippet, test a cart on your own checkout, since gateway filtering in the block checkout depends on the gateway plugin.

Related: hide payment methods by shipping method, and disable cash on delivery for some postcodes or order totals.

Tadrik Checkout Rules

Choose which payment methods each customer role sees. Free on WordPress.org.

See Tadrik Checkout Rules