在 Woocommerce 3 中获取地理位置的国家和地区

Posted

技术标签:

【中文标题】在 Woocommerce 3 中获取地理位置的国家和地区【英文标题】:Get the geo located Country and State in Woocommerce 3 【发布时间】:2018-08-13 10:28:31 【问题描述】:

我正在制作一个插件来显示估计的运输/交货时间,具体取决于访问者的 IP 地址。为此,我使用WC_Geolocation,但使用它我只能获得访客国家代码,如US CA 等,如果县是美国,我如何获得访客州名称。

 $geoData   = WC_Geolocation::geolocate_ip();
 $country   = $geoData['country'];
 echo $country;

将输出国家代码。如何获取州名?

【问题讨论】:

【参考方案1】:

要获得地理定位状态

// Get an instance of the WC_Geolocation object class
$geo_instance  = new WC_Geolocation();
// Get geolocated user geo data.
$user_geodata = $geo_instance->geolocate_ip();

// Get current user GeoIP Country
$country = $user_geodata['country'];

// Get current user GeoIP State
$state   = isset($user_geodata['state']) ? $user_geodata['state'] : '';
   

【讨论】:

如何找到包含 $user_geodata 的数组键,如 country state 如果你使用print_r($user_geodata)var_dump($user_geodata) 你会看到你得到一个country + state 的数组… 谢谢你拯救我的一天

以上是关于在 Woocommerce 3 中获取地理位置的国家和地区的主要内容,如果未能解决你的问题,请参考以下文章