Hide a payment method for certain countries or above a cart total, for example cash on delivery for large or overseas orders.
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;
} );
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.
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.
get_shipping_country() if that matters more for your rule.At checkout, change the billing country to one outside the US. Cash on delivery should disappear from the payment list.