skip to Main Content

I want to get shopify store visitor location through geolocation. I tried through this http://www.geoplugin.net/javascript.gp script but when I add this script into the shopify then site goes down. So I need proper solution for the shopify.

3

Answers


  1. You can make use of any IP to location service provider. consider below example.

    $.get("https://ipinfo.io/json", function (response) {
            console.log(response); // perform your action here
    },"jsonp");
    

    Add this script using Script Tag if you are creating an app. otherwise you can add this script on Shopify theme manually.

    Another way in the App proxy request shopify gives IP address/ As well as country data in request header.

    please follow below screenshot.

    enter image description here

    in php specif code you can use below code

    $headers = apache_request_headers();
    echo $headers['Cf-Ipcountry'];
    exit;
    
    Login or Signup to reply.
  2. After installing the shopify geolocation app and activating it for international market, use the endpoint "browsing_context_suggestions.json" to get the country name as:

    fetch('browsing_context_suggestions.json')
    .then(res=>res.json())
    .then(r=>console.log(r.detected_values.country.name))
    
    Login or Signup to reply.
  3. Shopify Does provide the localization feature. Which detects the local area and map and country. Use the following liquid code to get the list of countries and visitors local country name and code:

    <select>
    {%- for country in localization.available_countries -%}
    <option value="{{ country.iso_code }}" {%- if country.iso_code == localization.country.iso_code %} selected{% endif %}>
    {{ country.name }} ({{ country.currency.iso_code }} {{ country.currency.symbol }})
    </option>
    {%- endfor -%}
    </select>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search