问题
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