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.
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.
/**
* Disable WooCommerce payment gateway by user IP address.
*/
function get_our_ip_address() {
return array(
'000.00.000.000', // 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 {
unset(
$gateways['cod'], // Cash on delivery.
$gateways['cheque'], // Check payments.
$gateways['bacs'], // Direct bank transfer.
);
return $gateways;
}
}