Firefog
Firefog

Reputation: 3174

Get the geo located Country and State in Woocommerce 3

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?

Upvotes: 3

Views: 4225

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253901

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

Upvotes: 6

Related Questions