I’ve heard from multiple people, that the code provided by me doesn’t seem to work anymore. Please check the discussion in the Shopify forums for potential solutions and fixes.
I am currently working on an online store that’s running on Shopify. It’s my first project on this platform so naturally I find myself researching (googling) a lot to find answers to my burning questions. This time, my clients asked me to switch the shop’s currency automatically depending on the customer’s location.
On Shopify’s ecommerce university Discussion Forums I found a code snippet written by Ben Klinger that looked promising. People in the comments were reporting problems even after Ben delivered an update so I opened up my text editor and looked into it.
I also went ahead and added a teeny tiny bit of UX love to it, by having the script only run if the currency was not already set (either by the user or a previous check) in the cookie for the online store.
If this test is passed, the script fetches a JSON from https://freegeoip.net/ which – amongst other things – contains a 2-letter country code representing the country where the user is supposedly located. It then checks for a match within the set up country/currency pairs to determine whether or not to make a switch.
For this to work you will need a shop running on Shopify (duh!) and the widely used ‘jquery.currencies.js’ by Caroline Schnapp. If you got that covered already, just add the snippet somewhere after Caroline’s script either in your own .js-file or by embedding it directly in a Liquid template. Adjust the 2-letter country codes and 3-letter currency codes and you’re done.
I hope this will help you to improve the user experience of your own Shopify store! 🙂
Please note: This approach comes with a few caveats that I’d like to mention before you go:
- You are making a request to a third-party website (over HTTPS at least) on behalf of your users. This comes with privacy concerns of course which you should take into account before using it on either your or your client’s online stores.
- The third party in question doesn’t really offer an overly
reliable service. Timeouts happen every now and then and render the script useless. On the bright side: As long as the user doesn’t pick a currency by themselves, it will fire again after every pageload until it succeeds. I’ll update the gist and this blog posting if I find a better service to grab a JSON from. If you really depend on a 100% reliable solution: This is not for you.