在Woocommerce中,我使用的是this answer thread制成的代码,如果用户的GEO IP来自允许的多个国家/地区,则该代码将启用所有付款网关。在这里,我想要的允许的国家/地区代码为“ SE” (瑞典)。
如果GEO IP不在瑞典(允许的国家/地区)之外,我想禁用除BACS之外的所有支付网关。
感谢您的帮助。
答案 0 :(得分:0)
以下代码将为不允许的GEO IP定义的国家(瑞典在这里) 禁用BACS之外的所有可用支付网关:
// 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 ) {
if ( is_admin() ) return; // Only on front end
// ==> 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文件中。经过测试,可以正常工作。
相关:Disable payment gateways based on user country geo-ip in Woocommerce