Skip to content

Instantly share code, notes, and snippets.

@Drewberrysteph
Last active September 14, 2021 10:32
Show Gist options
  • 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>
@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