在我的 Woocommerce 商店中,我设置了地理定位系统,本地理定位识别出 IT 以外的任何国家/地区时,我想禁用支付方式
如果是IT(geop-ip),显示支付方式
如果所有其他国家/地区 (geo-ip),请禁用所有付款方式。
最佳答案
Woocommerce 已经通过 WC_Geolocation
class 提供了地理定位 IP 功能 ,因此您不需要任何额外的插件。
以下是根据客户地理定位的 IP 国家/地区禁用除“IT”(意大利) 国家/地区代码以外的所有国家/地区的支付网关的方法:
// 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 文件。经过测试并有效。
相关:
关于php - 在 WooCommerce 中禁用所有基于用户国家/地区 geo-ip 的付款方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954740/