Skip to content

Instantly share code, notes, and snippets.

@Drewberrysteph
Last active September 14, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drewberrysteph/e7f44d23d714617134ad7250d9149b06 to your computer and use it in GitHub Desktop.
Save Drewberrysteph/e7f44d23d714617134ad7250d9149b06 to your computer and use it in GitHub Desktop.
Shopify auto currency based on user's location (without needing to refresh)
<script>
function setCookie(key, value, expiry) {
var expires = new Date();
expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + ';path=/';
}
$(document).ready(function () {
// You need to enable the currency that you want to be supported under shopify settings/payment
var currency = {"GR":"EUR", "JP":"JPY", "PH":"PHP", "US":"USD"}; // Add capital and currency
$.get("https://ipinfo.io/json", (location) => {
var userCurrency = currency[location.country];
setCookie('cart_currency', userCurrency, '1')
},"jsonp");
});
</script>
@kwibis
Copy link

kwibis commented Feb 7, 2021

@Drewberrysteph thanks for the script. However, you do need an api-key as we will surpass the amount of requests allowed. Also, does this refresh our website?

@Drewberrysteph
Copy link
Author

@kwibis you can do that for other API's that requires key. No it does not.

@hammadCodes
Copy link

@kwibis you can do that for other API's that requires key. No it does not.

Can we use this logic to show divs on location base.. If yes, then can you please show show how?

@Drewberrysteph
Copy link
Author

@engr-hammad-uet, yes

setCookie('country', location.country, '1')

Then you get the country code in the cookie.

@kwibis
Copy link

kwibis commented Feb 20, 2021

@Drewberrysteph, thanks! I want to use the script, but not sure why to add capitals. Do you mean country code? If so, I'd like the website to always be EUR. except for the currencies added to that VAR list (AUD, USD, CAD, GBP, CHF, TTD, SEK).

@Drewberrysteph
Copy link
Author

@kwisis, sure, country code.

@paularupolo
Copy link

paularupolo commented May 20, 2021

Hi @Drewberrysteph, thanks for creating this!
I am trying to use it on my store (https://qimnuq0ewum00sz7-5518655558.shopifypreview.com) but can't seem to make it work. I changed GR for ES (as I am in Spain atm) but it doesn't change for me. I only want to detect the visitors IP and show the currencies in their $. Can you assist?

@Drewberrysteph
Copy link
Author

@paularupolo sorry for the late reply, check comment on line 9 and 10

@numzie
Copy link

numzie commented Aug 12, 2021

@Drewberrysteph - is there any way to do a default currency?

I have a Danish shop. It is in DKK for all Danes.
I support another 5 currencies, which I would match up with a country code.
However for all other countries I would like EURO as default - and I would very much like to avoid having to pair all countries in the world with EURO :)

@Drewberrysteph
Copy link
Author

@numzie try to replace line 12 with

var userCurrency = currency[location.country] ? currency[location.country] : "EUR" ;

@Jovdza012
Copy link

Jovdza012 commented Sep 10, 2021

I think you have an extra curly bracket on line 14. Anyhow, I still need to reload for the currency to switch.

`<script> 
  
  
   function setCookie(key, value, expiry) {
     var expires = new Date();
     expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
     document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + ';path=/';       
  }
    
  $(document).ready(function () {
     // You need to enable the currency that you want to be supported under shopify settings/payment
    var currency = {"IT":"EUR", "BR":"BRL", "CO":"COP", "ES":"EUR", "BO":"BOB","CL":"CLP","EC":"USD","GY":"GYD", "MX":"MXN", "PY":"PYG","PE":"PEN","PT":"EUR","SR":"SRD","UY":"UYU","VE":"USD"}; // Add capital and currency 
    $.get("https://ipinfo.io/json",  (location) => {
             var userCurrency = currency[location.country];
             console.log(userCurrencyconcat("JOVA"));
             setCookie('cart_currency', userCurrency, '1')
        },"jsonp");
  });

</script>`

This is my code, did do something wrong?

@Drewberrysteph
Copy link
Author

@Jovdza012 Thanks, try to clear cache before testing.

@Jovdza012
Copy link

I did, I am testing it with a VPN and with a clean browser every time (I use the guest account on chrome). I put it in theme.liquid file, below the Facebook tracking code, and before the theme loads its CSS :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment