获取Woocommerce 3中的地理位置地理位置

时间:2018-08-13 10:28:32

标签: php wordpress woocommerce geolocation geoip

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

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

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

1 个答案:

答案 0 :(得分:2)

要获取地理位置

// 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   = $user_geodata['state'];