Here we discuss how to disable payment gateways as per wordpress user role. Sometimes we need to disable any payment option as per user role lets disable cash on delivery (COD) for the user role “customer”.
If you need any custom woocommerce plugin development or any woocommerce customization please contact me. I’ll be happy to help you.
1st of all we need to know how many available payment option is there and how many user role is there so for this we can use wordpress default filter like woocommerce_available_payment_gateways
and function like current_user_can()
.
How to find WooCommerce Payment Gateway ID
In our woocommerce settings panel payment tab there we can see all of our payment method but basically we can see only the payment option name not the ID. We need to know the ID which is use in all background code so this is our 1st target to know all payment gateway ID. 2 easy option to find the gateway ID.
1st you can just inspect element the woocommerce Payment methods table and see the ID from “data-gateway_id” HTML tag. Just copy the value of that tag.

So here are the default gateway ID list :
- Direct bank transfer – bacs
- Cash on delivery – cod
- Check payments – cheque
- PayPal Standard – paypal
2nd option is to find it from the URL slug. Go to Woocommerce >> Settings >> Payment >> Now just open up any payment method by clicking on the name. So you will redirected to respective gateway page now see the URL it’s looks like this https://YOUR DOMAIN/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=cod. This is for Cash on delivery. Here “cod” is your payment gateway ID. So you can find ID same way for other gateway also.
All Default wordpress user role
Shop Manager: shop_manager
Customer: customer
Subscriber: subscriber
Contributor: contributor
Author: author
Editor: editor
Administrator: administrator
Let you need to disable cod(cash on delivery) for the user role customer and remain open for the other user role. So if any customer user type order then cod option will not available on checkout, below code will help you to do this functionality. You need to put this on your main theme or child theme function.php
page.
Disable Payment Gateways As Per WordPress User Role
add_filter( 'woocommerce_available_payment_gateways', 'a2zwp_turn_off_cod_payment' );
function a2zwp_turn_off_cod_payment( $available_gateways ) {
if( current_user_can( 'customer' ) ) {
if ( isset( $available_gateways[ 'cod' ] ) ) {
unset( $available_gateways[ 'cod' ] );
}
}
return $available_gateways;
}
You can disable payment option for multiple role like this way
current_user_can( 'subscriber' ) || current_user_can( 'editor' )
Disable Payment Gateway for Non Registered Users Or Guest User
Some time we are allowing the guest checkout but we still need to disable some payment option for guest checkout so below code snippet will help you .
add_filter( 'woocommerce_available_payment_gateways', 'a2zwp_turn_off_cod_payment' );
function a2zwp_turn_off_cod_payment( $available_gateways ) {
if( ! is_user_logged_in() ) {
if ( isset( $available_gateways[ 'cod' ] ) ) {
unset( $available_gateways[ 'cod' ] );
}
}
return $available_gateways;
}
Buy customize plugin or any woocommerce addon plugin please Click Here.