Skip to content

Instantly share code, notes, and snippets.

@amunim
Last active March 19, 2024 13:07
Show Gist options
  • Save amunim/9ea75f15d96d6165134795c78878dcd9 to your computer and use it in GitHub Desktop.
Save amunim/9ea75f15d96d6165134795c78878dcd9 to your computer and use it in GitHub Desktop.
Change currency on the page
let _myCountry = localStorage.getItem("_myCountry");
let _myExchange = localStorage.getItem("exchange");
let _myExchangePair = localStorage.getItem("exchangePair");
let store_language = "english";
let nowCurr = store_currency.trim();
var hours = 72;
var now = new Date().getTime();
var setupTime = localStorage.getItem("setupTime");
if (setupTime == null) {
localStorage.setItem("setupTime", now);
} else {
if (now - setupTime > hours * 60 * 60 * 1000) {
localStorage.setItem("exchange", "");
_myExchange = null;
localStorage.setItem("setupTime", now);
}
}
const currencyCountryCodes = {
"SA": {
"english": "SAR",
"arabic": "ريال سعودي"
},
"US": {
"english": "USD",
"arabic": "دولار أمريكي"
},
"KW": {
"english": "KWD",
"arabic": "د.ك"
},
"BH": {
"english": "BHD",
"arabic": "دينار بحريني"
},
"OM": {
"english": "OMR",
"arabic": "ريال عماني"
},
"EU": {
"english": "EUR",
"arabic": "يورو"
},
"GB": {
"english": "GBP",
"arabic": "جنيه إسترليني"
},
"EG": {
"english": "EGP",
"arabic": "جنيه مصري"
},
"TR": {
"english": "TRY",
"arabic": "ليرة تركية"
},
"CA": {
"english": "CAD",
"arabic": "دولار كندي"
},
"AU": {
"english": "AUD",
"arabic": "دولار أسترالي"
},
"JO": {
"english": "JOD",
"arabic": "دينار أردني"
},
"QA": {
"english": "QAR",
"arabic": "ريال قطري"
},
"MY": {
"english": "MYR",
"arabic": "رينغيت ماليزي"
},
"SE": {
"english": "SEK",
"arabic": "كرونا سويدية"
},
"PK": {
"english": "PKR",
"arabic": "روبية باكستانية"
},
"AE": {
"english": "AED",
"arabic": "دإ‏"
}
};
function setExchangeRate(pair, rate) {
_myExchange = rate;
localStorage.setItem("exchange", rate);
_myExchangePair = pair;
localStorage.setItem("exchangePair", pair);
}
function setCountry(country) {
localStorage.setItem("_myCountry", country);
_myCountry = country;
}
function updatePrices() {
let rate = 1;
if (!_myExchange) return;
rate = Number.parseFloat(_myExchange);
nowCurr = currencyCountryCodes[_myCountry].english;
$("body *").each(function () {
if (!$(this).clone()
.children()
.remove()
.end()
.text()
.match(/((\d{1,3},)+)*(\d{1,3})(\.\d{2,3})/))
return;
const reg = /((\d{1,3},)+)*(\d{1,3})(\.\d{2})/;
const num = reg.exec($(this).text());
const newPrice = ((Number.parseFloat(num[0].replaceAll(",", ""))) * rate).toFixed(2);
$(this).text(currencyCountryCodes[_myCountry][store_language] + " " + newPrice);
})
}
function selectedCountryDisplay() {
let currencyText = "Currency";
if (store_language == "arabic")
currencyText = "العملة";
$(".country-text").children().first().text(currencyText);
$(".country-text").children().last().text(currencyCountryCodes[_myCountry][store_language]);
$("img[alt='currency-country-active']").attr("src", `https://media.zid.store/static/${_myCountry.toLowerCase()}.svg`);
$("button[id='destinationCountry']").text(currencyCountryCodes[_myCountry][store_language]);
}
if (!_myCountry || _myCountry == null)
$.get("https://ipinfo.io", function (response) {
_myCountry = response.country;
}, "jsonp");
function load() {
for (const codes of Object.keys(currencyCountryCodes)) {
if (currencyCountryCodes[codes].arabic == nowCurr) {
store_language = "arabic"
nowCurr = currencyCountryCodes[codes].english;
}
}
$(document).off("click", ".change-country");
selectedCountryDisplay();
$(".dev3-dropdown-menu").empty();
$(".dev3-dropdown-menu").append($(Object.keys(currencyCountryCodes).map(x =>
`<li>
<a href="#" data-code="${x}" class="change-country">
<img class="menu-icons rounded-circle me-1" src="https://media.zid.store/static/${x.toLowerCase()}.svg"
width="20" height="20" alt="currency-country">
${currencyCountryCodes[x][store_language]}
</a>
</li>`).join("\n")));
$("#destinationCountries div").empty();
$("#destinationCountries div").append($(Object.keys(currencyCountryCodes).map(x =>
`<span class="change-country" id="${currencyCountryCodes[x].english}" data-code="${x}" onclick="hideAllDestinationsDropDowns();">
${currencyCountryCodes[x][store_language]}
</span>`).join("\n")));
let currencyText = "Currency";
if (store_language == "arabic")
currencyText = "العملة";
$(".destination-shipping-language").first().text(currencyText);
$("button[id='destinationCountry']").text(currencyCountryCodes[_myCountry][store_language]);
$("#modal-select-destination-city-save").replaceWith(`<button id="modal-select-destination-city-save">
Save
</button>`);
$("form[action='/customer/lang-shipping-country']").submit(function (e) {
e.preventDefault();
return false;
});
$("form[action='/customer/lang-shipping-country']").attr("method", "");
$("form[action='/customer/lang-shipping-country']").attr("action", "");
$(document).on("click", "#modal-select-destination-city-save", function () {
$("#langCurrecyModal").attr("class", "modal fade");
$("#langCurrecyModal").attr("style", "display: none;");
$("body").attr("style", "");
$(".modal-backdrop.fade.show").remove();
})
$(document).on("click", ".change-country", function () {
setCountry($(this).data("code"))
selectedCountryDisplay();
checkAndUpdate();
});
selectedCountryDisplay();
checkAndUpdate()
};
load();
function checkAndUpdate() {
const pair = `${store_currency}/${currencyCountryCodes[_myCountry].english}`;
if (_myExchangePair == pair && _myExchange) {
updatePrices();
return;
}
if (Object.keys(currencyCountryCodes).indexOf(_myCountry) > -1) {
fetch(`https://exchange-bwxvbry5lq-uc.a.run.app/?to=${currencyCountryCodes[_myCountry].english.toUpperCase()}&from=${nowCurr.toUpperCase()}`)
.then(x => x.json())
.then(
function (response) {
if (response.message == "Validation error") return;
setExchangeRate(pair, response.data[currencyCountryCodes[_myCountry].english].value);
updatePrices();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment