Last active
December 17, 2017 00:41
-
-
Save dono-shopify/318543a5e57a42ea32a5 to your computer and use it in GitHub Desktop.
Multiple Languages + Currencies
This file contains 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
<!-- This is an example of automatic currency switching based on IP as well as language selection using Langify and redirects. | |
All domains should be set up in Langify with their appropriate languages. I'd recommend placing this code in its own snippet | |
and including it near the </body> tag in your theme.liquid | |
Dependencies: | |
- currencies.js (https://github.com/carolineschnapp/currencies) | |
- Langify (https://apps.shopify.com/langify) | |
- A Shopify Store (well, should work anywhere, but only tested on Shopify) | |
--> | |
<script> | |
function redirectToCountry() { | |
if (window.location.hostname !== "onboardingtest.com") { | |
return; | |
} | |
jQuery.ajax({ | |
url: '//freegeoip.net/json/', | |
type: 'GET', | |
dataType: 'jsonp', | |
success: function(location) { | |
if (location.country_code == 'CA') { // Change domain based on location | |
window.location.hostname = 'ca.onboardingtest.com'; | |
} else if (location.country_code == 'AU') { | |
window.location.hostname = 'au.onboardingtest.com'; | |
} else if (location.country_code == 'US') { | |
window.location.hostname = 'us.onboardingtest.com'; | |
} else { // Fallback if location not set | |
window.location.hostname = 'global.onboardingtest.com'; | |
} | |
} | |
}); | |
} | |
function changeCurrency() { // Set currency based on domain | |
if (window.location.hostname == "ca.onboardingtest.com") { | |
$("#currencies").val("CAD"); | |
$("#currencies").change(); | |
} else if (window.location.hostname == "au.onboardingtest.com") { | |
$("#currencies").val("AUD"); | |
$("#currencies").change(); | |
} else if (window.location.hostname == "us.onboardingtest.com") { | |
$("#currencies").val("USD"); | |
$("#currencies").change(); | |
} else { | |
$("#currencies").val("EUR"); | |
$("#currencies").change(); | |
} | |
} | |
$(document).ready(function() { | |
redirectToCountry() | |
changeCurrency() | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment