在 WooCommerce 中禁用所有基于用户国家/地区 geo-ip 的付款方式
Disable all payment methods based on user country geo-ip in WooCommerce
在我的 Woocommerce 商店中,我设置了地理定位系统,当地理定位识别出 IT 以外的任何国家/地区时,我想禁用付款方式
如果是 IT (geop-ip),显示付款方式
如果所有其他国家/地区 (geo-ip),请禁用所有付款方式。
为了找出用户所在的国家/地区,您可以使用 FreeGeoIp, now renamed to Ipstack 这样的工具。您向该服务提供一个 IP 地址,它会告诉您用户可能所在的国家/地区地址(以及其他信息)。
有两种选择
1. 使用他们托管的 API(10,000 次请求免费,超过 10,000 次则付费)
2. 从 GitHub link 下载一个版本并自己托管
当您需要了解用户的国家/地区时,您可以将带有用户 IP 地址的 HTTP 请求发送到 API,然后使用该信息启用或禁用支付方式。
我知道Istack,还有maxmind等..
我想像这个功能更简单,它基于 blling_country 而不是 geo-ip 国家:
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( is_admin() ) return;
if ( isset( $available_gateways['authorize'] ) && $woocommerce->customer->get_billing_country() <> 'US' ) {
unset( $available_gateways['authorize'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_billing_country() == 'US' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
Woocommerce 已经具有地理定位 Ip 功能 到 WC_Geolocation
class,因此您不需要任何额外的插件。
这是禁用除“IT”以外的所有国家/地区支付网关的方法 (意大利) 国家/地区代码,基于客户地理定位的 IP 国家/地区:
// Disabling payment gateways except for the defined country codes based on user IP geolocation country
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// ==> HERE define your country codes
$allowed_country_codes = array('IT');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// Disable payment gateways for all countries except the allowed defined coutries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) )
$available_gateways = array();
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关:
- Enabling Payment method based on the customers location
这是@LoicTheAztec 答案的一个变体,它只禁用一种特定的支付方式,而不是全部:
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// ==> HERE define your country codes
$allowed_country_codes = array('DE','AT');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// Disable payment gateways for all countries except the allowed defined coutries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ) {
unset( $available_gateways['stripe_sofort'] );
}
return $available_gateways;
}
在我的 Woocommerce 商店中,我设置了地理定位系统,当地理定位识别出 IT 以外的任何国家/地区时,我想禁用付款方式
如果是 IT (geop-ip),显示付款方式
如果所有其他国家/地区 (geo-ip),请禁用所有付款方式。
为了找出用户所在的国家/地区,您可以使用 FreeGeoIp, now renamed to Ipstack 这样的工具。您向该服务提供一个 IP 地址,它会告诉您用户可能所在的国家/地区地址(以及其他信息)。
有两种选择 1. 使用他们托管的 API(10,000 次请求免费,超过 10,000 次则付费) 2. 从 GitHub link 下载一个版本并自己托管
当您需要了解用户的国家/地区时,您可以将带有用户 IP 地址的 HTTP 请求发送到 API,然后使用该信息启用或禁用支付方式。
我知道Istack,还有maxmind等.. 我想像这个功能更简单,它基于 blling_country 而不是 geo-ip 国家:
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( is_admin() ) return;
if ( isset( $available_gateways['authorize'] ) && $woocommerce->customer->get_billing_country() <> 'US' ) {
unset( $available_gateways['authorize'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_billing_country() == 'US' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
Woocommerce 已经具有地理定位 Ip 功能 到 WC_Geolocation
class,因此您不需要任何额外的插件。
这是禁用除“IT”以外的所有国家/地区支付网关的方法 (意大利) 国家/地区代码,基于客户地理定位的 IP 国家/地区:
// Disabling payment gateways except for the defined country codes based on user IP geolocation country
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// ==> HERE define your country codes
$allowed_country_codes = array('IT');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// Disable payment gateways for all countries except the allowed defined coutries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) )
$available_gateways = array();
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关:
- Enabling Payment method based on the customers location
这是@LoicTheAztec 答案的一个变体,它只禁用一种特定的支付方式,而不是全部:
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// ==> HERE define your country codes
$allowed_country_codes = array('DE','AT');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// Disable payment gateways for all countries except the allowed defined coutries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ) {
unset( $available_gateways['stripe_sofort'] );
}
return $available_gateways;
}