Limit WooCommerce Payment Gateways Based on country

In some cases you would like to limit the payment gateways your WooCommerce store offers based on the customer’s billing country. The following code snippet uses the woocommerce_available_payment_gateways filter to accomplish this.

First, you will need to find and copy the payment gateway stub by going to: WP-Admin > WooCommerce > Settings > Payments > [select the gateway] then locate the stub in the address bar.

wp-admin/admin.php?page=wc-settings&tab=checkout&section=[Payment Gateway Stub]

Then you can modify the following code snippet to your the functions.php file of your child theme or the code snippets plugin. Replace both {Payment Gateway Stub} with what you copied in the step above.

This is set up to include this gateway option for customers with a United States or Canadian billing address only; but, it can be modified to include or exclude based on any parameter.

add_filter( 'woocommerce_available_payment_gateways', 'yourprefix_payment_gateway_enable_country' );
 function yourprefix_payment_gateway_enable_country( $available_gateways ) {
     if (is_admin()){return $available_gateways;}
     if (isset($available_gateways['{Payment Gateway Stub}']) && !in_array(WC()->customer->get_billing_country(), array('US','CA'))) {
         unset( $available_gateways['{Payment Gateway Stub}''] );
     } 
     return $available_gateways;
 }

This code is here for reference purposes only and is not intended for use in a production environment. Modify and use this code at your own risk.

Leave a comment

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