Last active
January 16, 2023 20:04
-
-
Save chrisgrabinski/db99b2ac12641fbf3b8e to your computer and use it in GitHub Desktop.
(Shopify) Automatically change a shop's currency based on the customer's geolocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
@leeban17 your code has been working great! Any idea on how to work with the new currency selector from shopify?
@leeban17 your code has been working great! Any idea on how to work with the new currency selector from Shopify?
I built a free auto currency and location app that works with the new currency & region selector of Shopify themes. It's inspired by discussions here with all code optimized for performance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for that. Seems though that it only works second time. The first time you go to a link it will not change currency. If you refresh, then it will. Is there any way to fix this? Thanks!