Getting user's country code

Solved
Nor1
Tourist
12 0 4

Dear all,

 

do you know a good javascript solution to get user's country code? I need to make several custom notes based on the location of the visitor.

I tried searching around and this seemed to be a solution but somehow it doesnt work when i add it to my shopify store.

 

<script>
$.get("http://ipinfo.io", function(response) {
console.log(response.city, response.country);
}, "jsonp");

</script>

 

Do you know what could be the reason for that? The error message says: Uncaught ReferenceError: $ is not defined.

 

Or if you might have another working solution (but no apps), i would be very grateful.

 

Thanks!

0 Likes
DevsIT
Shopify Expert
45 2 9

-----------

If helpful then please Like and Accept Solution.
Want to modify or custom changes on store Hire me.
- Feel free to contact me on ce.shahabuddin@gmail.com regarding any help
Certified Shopify Expert | Skype : engr-shahabuddin | Whatsapp: +8801722574364
0 Likes
DevsIT
Shopify Expert
45 2 9

Hi @Nor1,

Can you share your site link? We can help you to give solution.

Even customization support for it.

If helpful then please Like and Accept Solution.
Want to modify or custom changes on store Hire me.
- Feel free to contact me on ce.shahabuddin@gmail.com regarding any help
Certified Shopify Expert | Skype : engr-shahabuddin | Whatsapp: +8801722574364
0 Likes
Speedimize
Shopify Partner
303 48 146

This is an accepted solution.

Hi @Nor1 

Hope this message finds you well and safe. 

Aibek is here from Speedimize.io 

The error indicates that you do not have jQuery. https://www.w3schools.com/jquery/jquery_get_started.asp 

You can connect it via cdn before you receive the country data. That is, it should be as below:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$.get("https://ipinfo.io", function(response) { // we changed to https
console.log(response.city, response.country);
}, "jsonp");
</script>

There may still be a problem that you have jQuery, but it connects asynchronously. In this case, check whether there are attributes such as async defer in the jQuery tag. 

Hope that helps you.

We Speedimize your store
Have a problem with speed performance, send me email here: hello@speedimize.io


If our advice was helpful please LIKE or ACCEPT SOLUTION
0 Likes
Nor1
Tourist
12 0 4

thanks a lot!!!

Actually, the problem was not in the jquery, but in the http -> https, as you indicated in your correction. After that, all worked flawlessly.

 

Thanks you

0 Likes
Jaspbur756
Tourist
14 0 2

HI ,

That was extremely useful information, thank you for sharing.

I am looking for something similar, in that, I wish to display on a Page in my Store the shipping speed specific to the User's country (not the Shipping Zone or Rate, just information on a page similar to About Us).

How would I display information in the middle of an About Us page that says the shipping speed to the USA is 10 days (for example)?

Thanks in Advance,

Happy. 

0 Likes