Disable a WooCommerce Payment Gateway by User IP Address

The code at the bottom of this post allows you to disable a WooCommerce payment gateway by the current user’s IP address. For example, you can disable the cash payment method if the current user’s IP address doesn’t match the IP address you predefined in the get_our_ip_address function.

Why disable a payment gateway by user IP address?

You can quickly create a test order on a live website by disabling the cash payment method for everyone but yourself. This can be especially helpful when you’re setting up Zapier and need to test your connection.

In a more unique scenario, you can use WooCommerce as a basic point-of-sale (POS) system. Using the User Switching plugin, you can log in as a customer, place an order under their name, and use the cash payment method (use your brick-and-mortar location’s IP address in the get_our_ip_address function). You would then accept your customer’s cash payment.

How to Add to Your Website

  1. Connect to your location’s internet connection.
  2. Get your IP address.
  3. Add your IP address to the get_our_ip_address function.
  4. Copy the functions (with your IP address) and paste them into your theme functions.php file or use WPCode.
  5. Make sure the payment gateway you would like to use in this scenario is active in the WooCommerce settings area.
 * Disable WooCommerce payment gateway by user IP address.
function get_our_ip_address() {
    return array(
        '', // Add your IP address.

add_filter( 'woocommerce_available_payment_gateways', 'enable_payment_gateway_by_user_ip_address' );
function enable_payment_gateway_by_user_ip_address( $gateways ) {

    // Get our IP address.
    $our_ip_address = get_our_ip_address();

    // Get the user's IP address.
    $user_ip = WC_Geolocation::get_ip_address();

    if ( in_array( $user_ip, $our_ip_address ) ) {
        return $gateways;
    } else {
            $gateways['cod'], // Cash on delivery.
            $gateways['cheque'], // Check payments.
            $gateways['bacs'], // Direct bank transfer.
        return $gateways;