get current visitor country code in shopify liquid file
Solution:
I know a way to get the country but not the code.
Let's assume you have an HTML element in some page that you want to display the country name inside
<p class="visitor-country-name"></p>
First install the free app (made by Shopify) called Geolocation (https://apps.shopify.com/geolocation).
Then put this JS code somewhere in that page inside < script > tags
fetch('browsing_context_suggestions.json')
.then(res=>res.json())
.then(r=> document.querySelector(".blog__title.h1").innerHTML = r.detected_values.country.name)
This will get the json file containing the visitor information, extract the 'detected_values.country.name' value from it, and inject it to the HTML element 'p' with the class name 'visitor-country-name'.
To see all available information visit the url below (replace 'store-name' with your store name)
store-name.myshopify.com/browsing_context_suggestions.json
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.