Skip to content

Instantly share code, notes, and snippets.

@NickGreen
Created February 3, 2021 20:54
Show Gist options
  • Save NickGreen/bc35efdd176794d39373fe27c373e0fa to your computer and use it in GitHub Desktop.
Save NickGreen/bc35efdd176794d39373fe27c373e0fa to your computer and use it in GitHub Desktop.
Get customer location
<?php
if( get_current_user_id() > 0 ) { // if customer exists already
$country = WC()->customer->get_billing_country();
} else { // if not-logged-in, need to use geolocation data
// 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'];
}
if ( "US" == $country ) {
// do US stuff here
} else {
// assume they are in EU, because store only sells to US and EU countries
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment