skip to Main Content

I am working on WooCommerce checkout page. I can set default country using filter in functions.php file for all users. That’s fine.

But I need to set country name in checkout page based on location for both logged in and logged out users. For an example, If a user from USA, the user will see USA is selected in the country dropdown list in checkout page. And If user form Canada, the user will see Canada is selected the country dropdown list in checkout page.

2

Answers


  1. you set to change the default country with this code.

    Steps to be done before you insert this code can:

    1- Install this plugin:
    https://wordpress.org/plugins/geoip-detect/

    2- Activate the GeoIP API (data sources): the service that will return the location for you. You cand o this inside the plugin settings.

    3- Insert this code into function.php:

    add_filter( 'default_checkout_country', 'change_default_checkout_country' );
    function change_default_checkout_country() 
      return do_shoartcode('[geoip_detect2 property="country.isoCode"]');
    }
    

    !!!! The code will not work without the plugin because it’s using a shortcode made by that plugin.

    But remember that this is for any user who doesn’t have a saved billing address because Woocommerce will consider it on the default country.

    Login or Signup to reply.
  2. As Woocommerce already includes a geolocation IP via WC_Geolocation Class, that will allow you to get and set user geolocated country as default checkout country. There is no need of any additional plugin.

    Note: WooCommerce allows shop owners to automatically geolocate customers and display tax rates and shipping methods specific to a customer’s location. In order to determine the correct location for a customer, as of version 3.9+, WooCommerce uses an integration with MaxMind Geolocation (and it is completely free to get a GeoIP Lite 2 license).

    See: MaxMind Geolocation Integration – Set a license key in WooCommerce

    enter image description here

    The code that will set by default the geolocated IP user country in checkout:

    add_action( 'default_checkout_country' , 'set_user_geoip_country_as_default_checkout_country', 900 );
    function set_user_geoip_country_as_default_checkout_country( $default_country ) {
        // Get an instance of the WC_Geolocation object class
        $geolocation     = new WC_Geolocation();
        // Get user IP
        $user_ip_address = $geolocation->get_ip_address();
        // Get user geolocated data.
        $user_geoip_data = $geolocation->geolocate_ip( $user_ip_address );
    
        if ( isset($user_geoip_data['country']) && ! empty($user_geoip_data['country']) ) {
            $default_country = $user_geoip_data['country']; // Set user geoIp country
        }
    
        return $default_country;
    }
    

    If you want to do the same for checkout State field, you will use additionally the following:

    add_action( 'default_checkout_state' , 'set_user_geoip_state_as_default_checkout_state' );
    function set_user_geoip_state_as_default_checkout_state( $default_state ) {
        // Get an instance of the WC_Geolocation object class
        $geolocation     = new WC_Geolocation();
        // Get user IP
        $user_ip_address = $geolocation->get_ip_address();
        // Get user geolocated data.
        $user_geoip_data = $geolocation->geolocate_ip( $user_ip_address );
    
        if ( isset($user_geoip_data['state']) && ! empty($user_geoip_data['state']) ) {
            $default_state = $user_geoip_data['state']; // Set user geoIp State
        }
        return $default_state;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Related:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search