WooCommerce PHP Intermediate Requires WooCommerce

Disable a Payment Method by Country or Cart Total

Hide a payment method for certain countries or above a cart total, for example cash on delivery for large or overseas orders.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 15 lines
add_filter( 'woocommerce_available_payment_gateways', function ( array $gateways ): array {
	if ( is_admin() || ! WC()->customer || ! WC()->cart ) {
		return $gateways;
	}

	$country = WC()->customer->get_billing_country();
	$total   = (float) WC()->cart->get_total( 'edit' );

	// Cash on delivery only inside the US and below 500.
	if ( isset( $gateways['cod'] ) && ( 'US' !== $country || $total > 500 ) ) {
		unset( $gateways['cod'] );
	}

	return $gateways;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Some payment methods only make sense in some situations, like cash on delivery for local orders or bank transfer for large ones. This snippet hides a payment method based on the customer’s country and the order total.

How it works

WooCommerce builds the list of payment methods shown at checkout with woocommerce_available_payment_gateways. Removing a key hides that method. The billing country and cart total come from the customer’s session, so the list updates when they change their address. The block-based Checkout uses the same list.

Gateway ids are cod (cash on delivery), bacs (bank transfer), cheque, and each payment plugin’s own id, shown in the URL of its settings page.

Things to watch

  • Always leave at least one method available, or customers can’t check out at all.
  • Use the shipping country instead with get_shipping_country() if that matters more for your rule.

How to test it

At checkout, change the billing country to one outside the US. Cash on delivery should disappear from the payment list.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only