How to Display Woocommerce Payment Methods Conditionally? (14 hacks)

In this post I’m going to show you how to display WooCommerce payment methods conditionally. And, best of all – we’re going to do it without any fancy plugin.

Video: How to Display Woocommerce Payment Methods Conditionally?

Requirements: How to Display Woocommerce Payment Methods Conditionally?

In order to make it work you need to paste the snippets shown below using a Code Snippets plugin (or your child theme’s functions.php file). That is, just paste the code and activate it.

If you’re a pro then take a look at the WpCodeBox plugin which is is a much better alternative to Code Snippets.

Now, some explanations for the code snippets here below:

  • Take a look at the comments inside the code
  • You need to change your payment methods and other slugs (categories, tags etc) if needed

How to disable all Woocommerce payment gateways?

The reason you would need to disable all payment gateways is that maybe you need it just for some catalogue or ordering system. Hence, use this small piece of code.

// Disable all Woocommerce payment gateways
add_filter( 'woocommerce_cart_needs_payment', '__return_false' );

And this is the result.

How to Display Woocommerce Payment Methods Conditionally?

How to Disable Woocommerce payment gateways for specific user roles?

Let’s imagine that your shop has a bunch of user roles. For example, customer, Gold member, Silver package etc. This snippet here allows you to disable Woocommerce payment gateways for specific user roles.

Pay attention to line 8 for user role and lines 11-15 for payment gateways to be removed. In this example customer can use onluy BACS and all other gateways are removed.

// Disable Woocommerce payment gateways for specific user roles
add_filter('woocommerce_available_payment_gateways', 'wpsh_hide_payment_roles', 1);
  function wpsh_hide_payment_roles($gateways)
  {
      $current_user = wp_get_current_user();
      $role = $current_user->roles;
      global $woocommerce;
      // Here goes your user role
      if ($role[0] == 'customer') {
		  
		// Add payment gateways you need to disable. In this example only BACS is activated
        unset($gateways['cheque']); // Cheque
		unset($gateways['cod']); // Cash on delivery
		unset($gateways['paypal']); // Paypal
	//	unset($gateways['bacs']); // Direct bank transfer (BACS)
      }
      return $gateways;
  }

How to Disable Woocommerce payment gateways for specific user roles (alternative method)?

If for some reason previous method didn’t work out for you then use this snippet here below.

// Disable Woocommerce payment gateways for specific user roles
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable:payment_for_roles' ); 
function wpsh_disable:payment_for_roles( $available_gateways ) {
	/* Here goes your user role */
	if( current_user_can( 'customer' ) ) {
	/* Here goes your payment gateway */
		if ( isset( $available_gateways[ 'paypal' ] ) ) {
			unset( $available_gateways[ 'paypal' ] );
		}
		/* If you don’t need to dsable more than one gateways, then delete next three rows. 
		If you need to add more gateways then just copy next three rows and replace your gateway name. */
	        if ( isset( $available_gateways[ 'bacs' ] ) ) {
			unset( $available_gateways[ 'bacs' ] );
		}
	}

	return $available_gateways;
}

How to Disable Woocommerce payment gateways for logged-out users?

Sometimes, if you would like to display WooCommerce payment methods conditionally, you need to set up some rules for your logged-out users.

For example, I have a customer who needed to hide direct bank payments (BACS) for logged-out users. So, if you need to disable some specific Woocommerce payment gateway for logged-out users then this snippet here helps you out.

Once again, pay attention to line 7 which defines roles that are allowed to use gateways. Other roles have no access to these gateways. In this example customer and administrator roles can use BACS and Check payments, all other user roles do not.

// Disable Woocommerce payment gateways for logged-out users
add_filter('woocommerce_available_payment_gateways', 'wpsh_disable_payment_for_logged_out_users', 90, 1); 
function wpsh_disable_payment_for_logged_out_users( $available_payment_gateways ) {
$user = wp_get_current_user();
  
	// Define roles that are allowed to use gateways. Other roles have no access to these gateways.
	$allowed_roles = array('customer', 'administrator');

	if (!array_intersect($allowed_roles, $user->roles )) {

	if (isset($available_payment_gateways['bacs'])) {
	unset($available_payment_gateways['bacs']);
	}
	if (isset($available_payment_gateways['cheque'])) {
unset($available_payment_gateways['cheque']);
		}
	}
	return $available_payment_gateways;
}

How to Disable Woocommerce Payment Methods for Specific Category?

Now let’s take a look on how to disable Woocommerce payment methods for specific category or categories. In this example, we’re going to remove Cash on delivery (COD), Paypal and Check payments (see lines 16-19) for categories Accessories and Music (see line 6).

Just replace the category slugs and edit payment gateways you need to be removed.

// Disable Woocommerce Payment Methods for Specific Category
add_filter('woocommerce_available_payment_gateways','wpsh_disable_payment_for_categories');
function wpsh_disable_payment_for_categories($gateways){

    global $woocommerce;
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

        // Add your product category ID numbers here below
        $category_ids = array( 48,47 );
	  
        $terms = get_the_terms( $values['product_id'], 'product_cat' );
        foreach ($terms as $term) {

            if(in_array($term->term_id,$category_ids)){

                // Define your payment gateways you would like to remove
                unset($gateways['cod']); // Cash on delivery
                unset($gateways['bacs']); // BACS
                unset($gateways['cheque']); // Cheque
			  	unset($gateways['paypal']); // Paypal
                break;
            }
        break;
        }
    }
    return $gateways;
}

How to hide Woocommerce payment gateways if “ship to different address” is selected?

Since we already know how to remove Woocommerce payment gateways if “ship to different address” is selected.

This time, pay attention to line 62 which leaves only Cash on delivery method active, all other payment gateways will be deactivated.

// Hide Woocommerce payment gateways if "ship to different address" is selected
add_action( 'wp_footer', 'wpsh_ship_to_different' );
function wpsh_ship_to_different() {
    // Work on only on checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ):

    // Remove "ship_different" custom WC session on load
    if( WC()->session->get('ship_different') ){
        WC()->session->__unset('ship_different');
    }
    // jQuery Ajax code below
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        var a = '#ship-to-different-address-checkbox', b = '';

        // Ajax function
        function triggerSTDA( value ){
             $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'ship_different_address',
                    'ship_different': value,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result); // For testing (to be removed)
                }
            });
        }

        $(a).on( 'change', function(){
            b = $(this).prop('checked') === true ? 'yes' : 'no';
            triggerSTDA( b );
        });
    });
    </script>
    <?php
    endif;
}

// The WordPress Ajax PHP receiver
add_action( 'wp_ajax_ship_different_address', 'get_ajax_ship_different_address' );
add_action( 'wp_ajax_nopriv_ship_different_address', 'get_ajax_ship_different_address' );
function get_ajax_ship_different_address() {
    if ( isset($_POST['ship_different']) ){
        WC()->session->set('ship_different', esc_attr($_POST['ship_different']));
        echo $_POST['ship_different'];
    }
    die();
}

// Show/hide payment gateways
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_gateways', 100, 1 );
function wpsh_hide_gateways( $available_gateways ) {
    if( WC()->session->get('ship_different') == 'yes' ) {
        foreach( $available_gateways as $gateways_id => $gateways ){
            if( $gateways_id !== 'cod' ) {
                unset($available_gateways[$gateways_id]);
            }
        }
    }
    return $available_gateways;
}

How to Disable Woocommerce Payment Gateways for Specific Shipping Methods?

Next let’s move to the shipping method based display rules. That is, I’m going to show you how to disable Woocommerce payment gateways for specific shipping methods.

In this example, we’re going to remove Paypal and Cash on delivery (see line 8) for Flat rate and Local pickup shipping methods (see line 11).

Now, if you don’t know how to find out your shipping method ID-s then take a look at the video above or make a right click on the shipping method, select Inspect and the find your shipping ID as shown on the screenshot.

Disable Woocommerce Payment Gateways for Specific Shipping Methods
// Disable Woocommerce Payment Gateways for Specific Shipping Methods
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_shipping', 10, 1 );

function wpsh_hide_payment_for_shipping( $available_gateways ) { 

    if ( ! ( is_checkout_pay_page() ) ) {
 	// Add your  paymente gateways you would like to disable
    $gateways_to_disable = array( 'paypal', 'cod' );
		
	// Set shipping methods		
    $shipping_methods = array( 'flat_rate:1', 'local_pickup:2');
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If shipping mehtod is selected then the payment gateway will be disabled
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}
else { return $available_gateways;
    }
}

How to Disable Woocommerce Payment Gateways for Specific Shipping Class?

Now it’s time to take a look at how to disable Woocommerce payment gateways for specific shipping class. For example, this snippet here below removes Paypal and Cash on delivery methods (see lines 9-13) if cart contains product with “Demo class” shipping class (see line 7).

// Disable Woocommerce Payment Gateways for Specific Shipping Class
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_specific_class' );

function wpsh_hide_payment_for_specific_class( $gateways ) {
    if( !is_admin() ) {
        foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
			// Replace demo-class with your own shipping class slug
            if ( 'demo-class' === $values[ 'data' ]->get_shipping_class() ) {
                if( isset( $gateways[ 'paypal' ] ) ) {
                    unset( $gateways[ 'paypal' ] );
                }
				 if( isset( $gateways[ 'cod' ] ) ) {
                    unset( $gateways[ 'cod' ] );
					 // Don’t remove this line
                    break; 
                }
            }
        }
    }
    return $gateways;  
}

How to Disable Woocommerce Payment Gateways if Coupon is Applied?

Let’s get creative now and see how to disable Woocommerce payment gateways if coupon is applied. Doesn’t matter what coupon because this time we will disable method for any coupon.

In this example I’m going to remove Cash on delivery and Check payment payment gateways only if a coupon is applied. If you would like to know how to disable Woocommerce payment gateways if a specific oupon is applied then take a look at next chapter below.

// Disable Woocommerce payment gateways if coupon is applied
// 
add_filter('woocommerce_available_payment_gateways', 'wpsh_remove_payment_if_coupon_applied');

function wpsh_remove_payment_if_coupon_applied($available_gateways)
{
    $coupons = WC()->cart->applied_coupons;
    foreach ($coupons as $coupon) {
		// Remove Cash on delivery
        if(isset($available_gateways['cod'])){
          unset($available_gateways['cod']);
        }
		// Remove Cheque
		 if(isset($available_gateways['cheque'])){
          unset($available_gateways['cheque']);
        }
    }
    return $available_gateways;
}

How to disable Woocommerce payment gateways if a specific coupon is applied?

In this example here below, BACS and COD payment methods (see lines 13 and 14) will be removed only if someone uses coupon “test20” (see line 10).

How to disable Woocommerce payment gateways if a specific coupon is applied?

So, if you would like to disable Woocommerce payment gateways if a specific coupon is applied, then just use this snippet here.

// Disable Woocommerce payment gateways if a specific coupon is applied
add_filter('woocommerce_available_payment_gateways', 'wpsh_remove_payment_for_coupon');

function wpsh_remove_payment_for_coupon($available_gateways){

    $coupons = WC()->cart->applied_coupons;

    foreach ($coupons as $coupon) {
		// Define your coupon code here
        if ($coupon == 'test20') { 
			
			// Define payment gateways to be removed
            unset($available_gateways['bacs']);
			unset($available_gateways['cod']);
        }
    }
    return $available_gateways;
}

How to disable Woocommerce payment gateways for specific products?

Almost there, with a couple of more snippets left. This time, we will disable Woocommerce payment gateways for specific products in your cart.

In this example, we will remove Paypal and COD methods (see lines 21-25) only if products with ID-numbers 14 and 32 (see line 11) are added to the cart.

// Disable Woocommerce payment gateways for specific products
add_filter( 'woocommerce_available_payment_gateways', 'rudr_payment_methods_by_product_ids' );

function rudr_payment_methods_by_product_ids( $gateways ){

	if( is_admin() ) {
		return $gateways;
	}
	
	// Add product IDs you would like to unset payment gateways for
	$product_ids = array( 14, 32);

	if( is_wc_endpoint_url( 'order-pay' ) ) {
		return $gateways;	
	}

	foreach ( WC()->cart->get_cart_contents() as $key => $cart_item ) {
		// count number of items if needed (optional) 
		if( in_array( $cart_item[ 'data' ]->get_id(), $product_ids ) ) {
			// Remove Paypal
			if( isset( $gateways[ 'paypal' ] ) ) {
				unset( $gateways[ 'paypal' ] );
			// Remove Cash on Delivery
			if( isset( $gateways[ 'cod' ] ) ) {
			unset( $gateways[ 'cod' ] );
				}
			break; // Don’t remove this line
			}
		}
	}
	return $gateways; 
}

How to disable Woocommerce payment methods based on cart total?

This time it’s a simple one: we’re going to disable Woocommerce payment methods based on cart total. That is, if the total is more than 80 (see line 8) euros then COD, BACS and Check payments will be removed (see lines 11-14).

// Disable Woocommerce payment methods based on cart total
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable_gateways_based_on_total', 1);
 
function wpsh_disable_gateways_based_on_total( $gateways ){
	global $woocommerce;
 
// Set your cart amount here. You can set rule based on "is more than" and "is less than". 
	if($woocommerce->cart->total > 80) {
	
	// Add payment gateways you need to disable. In this example only BACS is activated. You can remove the ones you don’t need
		unset($gateways['cod']);
		unset($gateways['cheque']);
	//	unset($gateways['paypal']);
		//	unset($gateways['bacs']);
	}
	return $gateways;
}

How to disable Woocommerce payment methods for a specific country?

Why would you need to disable Woocommerce payment methods for a specific country? Well, if my shop is in Europe, and you’re in USA then I don’t see any point to using Cash on delivery (COD) payment method.

Therefore, in this example, I will remove COD for USA (see line 12-15). Also, I will remove Paypal and Check payments for Estonia, Finland and Norway (see lines 19-24).

// Disable Woocommerce payment methods for a specific country

add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_countries', 10, 1 );

function wpsh_hide_payment_for_countries( $payment_gateways ) {
    if ( is_admin() ) return $payment_gateways;
	
    // Get country
    $customer_country = WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country();
    
    // Hide COD for USA
    if ( in_array( $customer_country, array( 'US' ) ) ) {       
        // Hide Cash on delivery for United States
        if ( isset( $payment_gateways['cod'] ) ) {
            unset( $payment_gateways['cod'] );
        }
    } 
	 // Hide Paypal and Cheque payments for Estonia, Finland and Norway
	    if ( in_array( $customer_country, array( 'EE', 'FI', 'NO' ) ) ) {       
        if ( isset( $payment_gateways['paypal'] ) ) {
            unset( $payment_gateways['paypal'] );
        }
			if ( isset( $payment_gateways['cheque'] ) ) {
            unset( $payment_gateways['cheque'] );
        }
    } 
    return $payment_gateways;
}

How to disable Woocommerce payment gateways for bacordered products?

Take a look at this snippet here below. This one will hide BACS and Paypal payment methods if cart contains a backordered product. Lines 9 and 10 are the ones to modify accordingly.

// Disable Woocommerce payment gateways for bacordered products
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_backordered_hide', 90, 1 );
function wpsh_backordered_hide( $available_gateways ) {
    if ( is_admin() )
        return $available_gateways;
    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            unset($available_gateways['bacs']); // Hide BACS payment gateway
			unset($available_gateways['paypal']); // Hide Paypal payment gateway
            break; // Stop the loop
        }
    }

    return $available_gateways;
}

How to add Payment Gateway Based Fees in Woocommerce?

Next part of our “How to display WooCommerce payment methods conditionally?” post is here. That means, I’ll show you how to add Payment Gateway Based Fees in Woocommerce. For example, let’s add two kinds of fees with the help of this code:

  • Fixed fee (2 euros) for using Cash on delivery fee (see line 15)
  • Percentage fee (5%) for using Paypal (see line 16).

Just add your own fees and payment methods, and you’re good to go.

How to add Payment Gateway Based Fees in Woocommerce?
// Add Payment Gateway Based Fees in Woocommerce
add_action( 'woocommerce_cart_calculate_fees', 'wpsh_add_handling_fee' );
function wpsh_add_handling_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    $chosen_payment_id = WC()->session->get('chosen_payment_method');

    if ( empty( $chosen_payment_id ) )
        return;

    $subtotal = $cart->subtotal;

    // Here you can set up your fees (fixed or percentage)
    $targeted_payment_ids = array(
        'cod' => 2, // Fixed fee
        'paypal' => 5 * $subtotal / 100, // Percentage fee
    );

    // Loop through defined payment Ids array
    foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
        if ( $chosen_payment_id === $payment_id ) {
            $cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
        }
    }
}
// Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'wpsh_refresh_checkout_page' );
function wpsh_refresh_checkout_page() {
    wc_enqueue_js( "jQuery( function($){
        $('form.checkout').on('change', 'input[name=payment_method]', function(){
            $(document.body).trigger('update_checkout');
        });
    });");
}

How to add Payment A Gateway Based Discount in Woocommerce?

The final snippet for today is here and this time we will take a look at how to add a payment gateway based discount in Woocommerce. In this example, we will add a 5% discount (see line 13) only if Cash on delivery is selected (see line 10).

If you need to change the label text (COD discount) then see line 21.

How to add Payment A Gateway Based Discount in Woocommerce?
// Add Payment A Gateway Based Discount in Woocommerce?

add_action( 'woocommerce_cart_calculate_fees','wpsh_cod_discount', 20, 1 );

function wpsh_cod_discount( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Define your own payment method here
	$payment_method = 'cod';

    // Set your discount percentagey
    $percent = 5; // This will give 5% discount

    $cart_total = $cart_object->subtotal_ex_tax;
    
    $chosen_payment_method = WC()->session->get('chosen_payment_method');  

    if( $payment_method == $chosen_payment_method ){
        
                   $label_text = __( "COD Discount" );
        
                   // Calculating percentage
                   $discount = number_format(($cart_total / 100) * $percent, 2);
                   // Adding the discount
                   $cart_object->add_fee( $label_text, -$discount, false );
    }
}

add_action( 'woocommerce_review_order_before_payment', 'wpsh_refresh_checkout' );

function wpsh_refresh_checkout(){
    // jQuery
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}

As you see, there is no need for a fancy plugin. Just some copy and paste is needed to display WooCommerce payment methods conditionally.

Useful Woocommerce tips

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 108