How to hide a WooCommerce payment gateway or a shipping method based on certain rules.

  1. Home
  2. /
  3. How
  4. /
  5. to
  6. /
  7. How to hide a WooCommerce payment gateway or a shipping method based on certain rules.

Shipping methods and payment gateways in WooCommerce are visible to everyone by default. For shipping methods, you can only control their availability using shipping zone s which will allow you to display certain shipping methods based on the customer’s address. On the other hand, payment gateways are visible to all customers regardless of their address.

But what if you want more flexibility? What if you need some payment gateways to be available to only one set of customers that meet some conditions? For example, you may need a payment gateway to be only available if the cart total is more than $100, or if the customer is paying from a specific country.

Table of Contents

By Code

If you have coding experience, you can use WooCommerce hooks to filter the payment gateways and shipping methods. To filter üayment methods, you will need to use the woocommerce_available_payment_gateways filter. For shipping methods, you can use the woocommerce_package_rates filter.

For instance, if you need to hide PayPal if the cart total is less than $100, you can do as following: 

<?php

function exclude_paypal_if_cart_is_less_than_100( $gateways ) {

	if ( WC()->cart->get_subtotal() < 100 ) {
		unset( $gateways['paypal'] );
	}

	return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'exclude_paypal_if_cart_is_less_than_100' );

On the other hand, if you need to hide a shipping method if the cart total is less than $100, then you can do the following:

<?php

function exclude_fedex_if_cart_is_less_than_100($rates) {
	if ( WC()->cart->get_subtotal() < 100 ) {
		unset( $rates['fedex'] );
	}

	return $rates;
}

add_filter( 'woocommerce_package_rates', 'exclude_fedex_if_cart_is_less_than_100');

You may notice the “paypal” and “fedex” strings, these are the keys of the payment gateway and shipping method, these keys are different for each gateway and method. You can find each key for each method by printing the $rates and $gateways packages, this is the most accurate way to know the keys.

By Plugin

If you have no development experience and want more flexibility out of the box, you can use a plugin to do the heavy work for you and have more complex rules, and by combining more rules together. For this purpose, you can use the Conditional Payments and Shipping for WooCommerce plugin. A free plugin that can exclude shipping methods and payment gateways based on multiple rules such as shipping class, package weight and cart totals.

Conditional Payments and Shipping for WooCommerce

After activating the plugin, you can go to WooCommerce -> Shipping Conditions or Payment Conditions and start creating the rules. The rules can be created by the following: 

  • Cart Total
  • Coupon Code
  • Customer
  • Package Total Weight
  • Billing Country
  • Shipping Class
  • Shipping Country

These rules can cover most of the cases, but if you need more flexibility, you can try WooCommerce Restricted Shipping and Payment Pro , the Pro version provides more rules such as Customer Role, Product Category, Product Tag, and Total Cart Quantity.

 

 

ABOUT THE AUTHOR: Waseem Founder and Lead Developer of WPRuby.

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *