LindsayKB
2/2/2022 - 1:02 AM

Change Language to Spanish Depending on Conditions

//Get current URL
var currentURL = window.location.href;

//Retrieve session storage item
var spanish = sessionStorage.getItem("spanish");

//If the code for Spanish ("es") is found in the URL, set the session storage item to "true"
if (currentURL.indexOf("/es") > -1 ) {
  if (spanish != "true") {
    sessionStorage.setItem('spanish', 'true');
  }
}
//Once the Shopify store redirects to third-party checkout platform, this code will retrieve the session storage item and change the language if necessary

$( document ).ready(function() {
  //Get URL
    var spanish = sessionStorage.getItem("spanish");
    var currentURL = window.location.href;
    if (spanish == "true") {
      //This page should be in Spanish
       if (currentURL.indexOf("&language=es") > -1) {
           //Do nothing. URL already has query parameter
       }
       else {
         //var newURL = window.location.href + "&language=es";
         //Add query parameter and reload page
         window.location.search += '&language=es';
       }
      //If its the Thank you page, clear session storage value
      if (currentURL.indexOf("thank-you") > -1) {
          sessionStorage.clear();
       }
    }
});