Auto change currency based on location [TUTORIAL]

Ben_Klinger
Excursionist
45 0 12

If you're using the currency switcher, I have a piece of code that will save your customers time and make them feel right at home.

You should add this as a script in your theme.liquid.

THIS ASSUMES THAT YOU ARE USING JQUERY - 99% YOU ARE.

<script>
   jQuery.getJSON('http://freegeoip.net/json/';, function(location) {
   // check if a user is from Israel
  if (location.country_code == 'IL') {
    $("#currencies").val("ILS");
    $("#currencies").change();
  }
  // add more countries here
}
</script>

Just make sure you added the currency in the theme settings so it can change to it.

This is a modification of the code shown here. What it basically does is change the selector based on geolocation.

Ben Klinger, Studio Ve
Ignacio_Diaz_Ar
New Member
4 0 0

Hi, Thanks for the tutorial but it's not working for me. I already have the currency switcher working on my site, but nothing happens when I include this script into theme.liquid. Is there anything I need to do prior to include it?

Thanks,
Nacho

0 Likes
Ben_Klinger
Excursionist
45 0 12

Did you check to see that this loads after the jquery script? Did you add more countries and not just Israel?

Use the chrome browser Hola extension to trick the website and think you're from a different country. 

Ben Klinger, Studio Ve
0 Likes
Ignacio_Diaz_Ar
New Member
4 0 0

Thanks for the quick response.

I Included the script at the end of the page and tried both replacing the country to my country and adding new variables, with no luck. I just used Hola extension with the same results...

0 Likes
Ben_Klinger
Excursionist
45 0 12

My Guess would be to remove ILS from your list since your store doesn't support it. Also try to add this to see if the script is running:

<script>
   jQuery.getJSON('http://freegeoip.net/json/';;, function(location) {
  if (location.country_code == 'ES') {
    alert("you are from spain");
  }
  // add more countries here
}
</script>

 

Ben Klinger, Studio Ve
0 Likes
Ignacio_Diaz_Ar
New Member
4 0 0
I tried both deleting ILS and the new script and I didn't get the alert neither... it's very strange I think...
0 Likes
C_K1
New Member
5 0 0

Hi, I have also pasted this at the bottom of my theme.liquid just before </body>. I had currency switcher working using the usual manual toggle buttons but as far as I can see   this auto switching code isnt working (tested using hola chrome extension)

Please let me know if anyone has managed to get it to work. Its really annoying that such obvious functionality isnt built into shopify.

 

0 Likes
Ben_Klinger
Excursionist
45 0 12

Hi, 

I've noticed that the script changed a bit using JSON. Try this:

<script>
jQuery.ajax( { 
  url: '//freegeoip.net/json/', 
  type: 'POST', 
  dataType: 'jsonp',
  success: function(location) {
    // If the visitor is browsing from Canada.
    if (location.country_code === 'CA') {
      // Change currency to CAD.
    $("#currencies").val("CAD");
    $("#currencies").change();
    }
  }
} );
<script>

 

 

Ben Klinger, Studio Ve
0 Likes
Rafael_Berti
New Member
6 0 0

I have also been trying to use your code but neither the original post or the latest one works on my site. My website is based on Minimal template. Ben, have you got any other suggestions?

Has anyone make this work?

0 Likes
Ben_Klinger
Excursionist
45 0 12

Hi Rafael,

Sorry - but I don't have any suggestions. Maybe some of the experts could help.

Ben Klinger, Studio Ve
0 Likes