Skip to content

Instantly share code, notes, and snippets.

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 saravanasksk/8a5757368e88be7c0c8d91e9006db991 to your computer and use it in GitHub Desktop.
Save saravanasksk/8a5757368e88be7c0c8d91e9006db991 to your computer and use it in GitHub Desktop.
(Shopify) Automatically change a shop's currency based on the customer's geolocation
// Dependencies
// - https://github.com/carolineschnapp/currencies
// Don't change currency if Cookie has been already set
if (Currency.cookie.read() == null) {
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'GET',
dataType: 'jsonp',
success: function(location) {
if (location.country_code == 'TH') { // Default shop currency
$('[name=currencies][value=THB]').attr('checked', 'checked');
} else if (location.country_code == 'JP') { // Secondary currency
$('[name=currencies][value=JPY]').attr('checked', 'checked');
} else { // Fallback currency
$('[name=currencies][value=USD]').attr('checked', 'checked');
}
// Let the scripts in 'currencies.liquid' handle the rest
$('[name=currencies]').change();
}
} );
}
// Based upon code by Ben Klinger (www.studiove.com)
// Source: https://ecommerce.shopify.com/c/shopify-discussion/t/auto-change-currency-based-on-location-tutorial-179134
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment