在 Woocommerce 中基于 geo-ip 国家/地区禁用除 BACS 之外的所有支付网关
Disable all payment gateways except BACS based on geo-ip country in Woocommerce
在 Woocommerce 中,我使用由 制作的代码,如果用户的 GEO IP 来自一系列允许的国家/地区,它会启用所有支付网关。这里我想要的允许的国家代码是 "SE" (Sweden)。
如果 GEO IP 在瑞典(预定义的允许国家/地区)之外,我想要禁用除 BACS 之外的所有支付网关。
感谢任何帮助。
以下代码将禁用除 BACS 之外的所有可用支付网关,用于不允许的 GEO IP 定义的国家/地区 (这里是瑞典):
// Disabling payment gateways (except BACS) 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('SE');
// 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 (except BACS) for all countries except the allowed defined countries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ){
$bacs_gateways = $available_gateways['bacs'];
$available_gateways = array();
$available_gateways['bacs'] = $bacs_gateways;
}
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关:
在 Woocommerce 中,我使用由
如果 GEO IP 在瑞典(预定义的允许国家/地区)之外,我想要禁用除 BACS 之外的所有支付网关。
感谢任何帮助。
以下代码将禁用除 BACS 之外的所有可用支付网关,用于不允许的 GEO IP 定义的国家/地区 (这里是瑞典):
// Disabling payment gateways (except BACS) 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('SE');
// 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 (except BACS) for all countries except the allowed defined countries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) ){
$bacs_gateways = $available_gateways['bacs'];
$available_gateways = array();
$available_gateways['bacs'] = $bacs_gateways;
}
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
相关: