Get the geo located Country and State in Woocommerce 3

一曲冷凌霜 提交于 2021-01-01 08:01:20

问题


I am making a plugin to display estimate shipping/delivery time depend on visitor IP address. To do this I am using WC_Geolocation but using this I can get only visitor country code like US CA etc how can I get visitor state name if county is US .

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

will output country code. how to get state name?


回答1:


To get the geolocated state:

// 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'] : '';
   


来源:https://stackoverflow.com/questions/51820223/get-the-geo-located-country-and-state-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!