Created
February 3, 2021 20:54
-
-
Save NickGreen/bc35efdd176794d39373fe27c373e0fa to your computer and use it in GitHub Desktop.
Get customer location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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