I would like to show/hide a fee in the WooCommerce checkout process depending on the location of the user.

For this i would like to first use the IP address of the user to determine the location and later the address he enters.

I came across the build-in Geolocation ability of WooCommerce with the following classes:

I'm able to get the users IP address with:

$geolocation = new WC_Geolocation();
$ipaddress = $geolocation->get_ip_address();

But i don't seem to be able to get the users country code. How would i go about this?

1 Answer 1


This is how I used WooCommerce to get the country code on a recent project, using the geolocate_ip method of the WC_Geolocation class.

$country = 'US';

if (class_exists('WC_Geolocation')) {
    $location = WC_Geolocation::geolocate_ip('', true, false);

    if (isset($location['country'])) {
        $country = $location['country'];

switch ($country) {
    case 'AU':
    case 'US':

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.