Personalized checkout and custom promotions with Shopify Scripts
I have some code (as seen below) that when the currency is changed to $USD or $CAN it will redirect the user to our US website so they can purchase on there. The one issue I am having is that when USD is selected on my rest of world site it redirects perfectly but then unless you reset the cookies you are forever stuck on the US store as the browser remembers your last selected currency. Which is also a problem for when I want to add the opposite code to my US site, as you'll get stuck in a death loop. Can anyone help or know a solution?
<script>
document.addEventListener("DOMContentLoaded", function () {
var currencySelector = document.querySelector("[name='currency']");
if (currencySelector) {
currencySelector.addEventListener("change", function () {
if (this.value === "USD" || this.value === "CAD") {
window.location.href = "https://celler8.us"; // Redirect to US store
}
});
}
// Auto-redirect visitors from US or Canada
fetch("https://ipapi.co/json/")
.then(response => response.json())
.then(data => {
if (data.country_code === "US" || data.country_code === "CA") {
window.location.href = "https://celler8.us"; // Redirect US & Canadian visitors
}
});
});
</script>
2m ago Learn the essential skills to navigate the Shopify admin with confidence. T...
By Shopify Feb 12, 2025Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025