Skip to content

Instantly share code, notes, and snippets.

@Arifursdev
Last active February 18, 2023 11:37
Show Gist options
  • Save Arifursdev/c529e9eb5c3230c4ba509bf204cdc7b0 to your computer and use it in GitHub Desktop.
Save Arifursdev/c529e9eb5c3230c4ba509bf204cdc7b0 to your computer and use it in GitHub Desktop.
shopify jquery.currencies.js in vanilla js
/*! js-cookie v3.0.1 | MIT */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self,function(){var n=e.Cookies,o=e.Cookies=t();o.noConflict=function(){return e.Cookies=n,o}}())}(this,(function(){"use strict";function e(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)e[o]=n[o]}return e}return function t(n,o){function r(t,r,i){if("undefined"!=typeof document){"number"==typeof(i=e({},o,i)).expires&&(i.expires=new Date(Date.now()+864e5*i.expires)),i.expires&&(i.expires=i.expires.toUTCString()),t=encodeURIComponent(t).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var c="";for(var u in i)i[u]&&(c+="; "+u,!0!==i[u]&&(c+="="+i[u].split(";")[0]));return document.cookie=t+"="+n.write(r,t)+c}}return Object.create({set:r,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var t=document.cookie?document.cookie.split("; "):[],o={},r=0;r<t.length;r++){var i=t[r].split("="),c=i.slice(1).join("=");try{var u=decodeURIComponent(i[0]);if(o[u]=n.read(c,u),e===u)break}catch(e){}}return e?o[e]:o}},remove:function(t,n){r(t,"",e({},n,{expires:-1}))},withAttributes:function(n){return t(this.converter,e({},this.attributes,n))},withConverter:function(n){return t(e({},this.converter,n),this.attributes)}},{attributes:{value:Object.freeze(o)},converter:{value:Object.freeze(n)}})}({read:function(e){return'"'===e[0]&&(e=e.slice(1,-1)),e.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(e){return encodeURIComponent(e).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}},{path:"/"})}));
window.Currency = window.Currency || {};
Currency.cookie = {
configuration: {
expires: 365,
path: '/',
domain: window.location.hostname
},
name: 'currency',
write: function(currency) {
Cookies.set(this.name, currency, this.configuration)
},
read: function() {
return Cookies.get(this.name)
},
destroy: function() {
Cookies.remove(this.name, this.configuration)
}
};
Currency.moneyFormats = {
"USD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} USD"
},
"EUR":{
"money_format":"&euro;{{amount}}",
"money_with_currency_format":"&euro;{{amount}} EUR"
},
"GBP":{
"money_format":"&pound;{{amount}}",
"money_with_currency_format":"&pound;{{amount}} GBP"
},
"CAD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} CAD"
},
"ALL":{
"money_format":"Lek {{amount}}",
"money_with_currency_format":"Lek {{amount}} ALL"
},
"DZD":{
"money_format":"DA {{amount}}",
"money_with_currency_format":"DA {{amount}} DZD"
},
"AOA":{
"money_format":"Kz{{amount}}",
"money_with_currency_format":"Kz{{amount}} AOA"
},
"ARS":{
"money_format":"${{amount_with_comma_separator}}",
"money_with_currency_format":"${{amount_with_comma_separator}} ARS"
},
"AMD":{
"money_format":"{{amount}} AMD",
"money_with_currency_format":"{{amount}} AMD"
},
"AWG":{
"money_format":"Afl{{amount}}",
"money_with_currency_format":"Afl{{amount}} AWG"
},
"AUD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} AUD"
},
"BBD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} Bds"
},
"AZN":{
"money_format":"m.{{amount}}",
"money_with_currency_format":"m.{{amount}} AZN"
},
"BDT":{
"money_format":"Tk {{amount}}",
"money_with_currency_format":"Tk {{amount}} BDT"
},
"BSD":{
"money_format":"BS${{amount}}",
"money_with_currency_format":"BS${{amount}} BSD"
},
"BHD":{
"money_format":"{{amount}}0 BD",
"money_with_currency_format":"{{amount}}0 BHD"
},
"BYR":{
"money_format":"Br {{amount}}",
"money_with_currency_format":"Br {{amount}} BYR"
},
"BZD":{
"money_format":"BZ${{amount}}",
"money_with_currency_format":"BZ${{amount}} BZD"
},
"BTN":{
"money_format":"Nu {{amount}}",
"money_with_currency_format":"Nu {{amount}} BTN"
},
"BAM":{
"money_format":"KM {{amount_with_comma_separator}}",
"money_with_currency_format":"KM {{amount_with_comma_separator}} BAM"
},
"BRL":{
"money_format":"R$ {{amount_with_comma_separator}}",
"money_with_currency_format":"R$ {{amount_with_comma_separator}} BRL"
},
"BOB":{
"money_format":"Bs{{amount_with_comma_separator}}",
"money_with_currency_format":"Bs{{amount_with_comma_separator}} BOB"
},
"BWP":{
"money_format":"P{{amount}}",
"money_with_currency_format":"P{{amount}} BWP"
},
"BND":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} BND"
},
"BGN":{
"money_format":"{{amount}} лв",
"money_with_currency_format":"{{amount}} лв BGN"
},
"MMK":{
"money_format":"K{{amount}}",
"money_with_currency_format":"K{{amount}} MMK"
},
"KHR":{
"money_format":"KHR{{amount}}",
"money_with_currency_format":"KHR{{amount}}"
},
"KYD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} KYD"
},
"XAF":{
"money_format":"FCFA{{amount}}",
"money_with_currency_format":"FCFA{{amount}} XAF"
},
"CLP":{
"money_format":"${{amount_no_decimals}}",
"money_with_currency_format":"${{amount_no_decimals}} CLP"
},
"CNY":{
"money_format":"&#165;{{amount}}",
"money_with_currency_format":"&#165;{{amount}} CNY"
},
"COP":{
"money_format":"${{amount_with_comma_separator}}",
"money_with_currency_format":"${{amount_with_comma_separator}} COP"
},
"CRC":{
"money_format":"&#8353; {{amount_with_comma_separator}}",
"money_with_currency_format":"&#8353; {{amount_with_comma_separator}} CRC"
},
"HRK":{
"money_format":"{{amount_with_comma_separator}} kn",
"money_with_currency_format":"{{amount_with_comma_separator}} kn HRK"
},
"CZK":{
"money_format":"{{amount_with_comma_separator}} K&#269;",
"money_with_currency_format":"{{amount_with_comma_separator}} K&#269;"
},
"DKK":{
"money_format":"{{amount_with_comma_separator}}",
"money_with_currency_format":"kr.{{amount_with_comma_separator}}"
},
"DOP":{
"money_format":"RD$ {{amount}}",
"money_with_currency_format":"RD$ {{amount}}"
},
"XCD":{
"money_format":"${{amount}}",
"money_with_currency_format":"EC${{amount}}"
},
"EGP":{
"money_format":"LE {{amount}}",
"money_with_currency_format":"LE {{amount}} EGP"
},
"ETB":{
"money_format":"Br{{amount}}",
"money_with_currency_format":"Br{{amount}} ETB"
},
"XPF":{
"money_format":"{{amount_no_decimals_with_comma_separator}} XPF",
"money_with_currency_format":"{{amount_no_decimals_with_comma_separator}} XPF"
},
"FJD":{
"money_format":"${{amount}}",
"money_with_currency_format":"FJ${{amount}}"
},
"GMD":{
"money_format":"D {{amount}}",
"money_with_currency_format":"D {{amount}} GMD"
},
"GHS":{
"money_format":"GH&#8373;{{amount}}",
"money_with_currency_format":"GH&#8373;{{amount}}"
},
"GTQ":{
"money_format":"Q{{amount}}",
"money_with_currency_format":"{{amount}} GTQ"
},
"GYD":{
"money_format":"G${{amount}}",
"money_with_currency_format":"${{amount}} GYD"
},
"GEL":{
"money_format":"{{amount}} GEL",
"money_with_currency_format":"{{amount}} GEL"
},
"HNL":{
"money_format":"L {{amount}}",
"money_with_currency_format":"L {{amount}} HNL"
},
"HKD":{
"money_format":"${{amount}}",
"money_with_currency_format":"HK${{amount}}"
},
"HUF":{
"money_format":"{{amount_no_decimals_with_comma_separator}}",
"money_with_currency_format":"{{amount_no_decimals_with_comma_separator}} Ft"
},
"ISK":{
"money_format":"{{amount_no_decimals}} kr",
"money_with_currency_format":"{{amount_no_decimals}} kr ISK"
},
"INR":{
"money_format":"Rs. {{amount}}",
"money_with_currency_format":"Rs. {{amount}}"
},
"IDR":{
"money_format":"{{amount_with_comma_separator}}",
"money_with_currency_format":"Rp {{amount_with_comma_separator}}"
},
"ILS":{
"money_format":"{{amount}} NIS",
"money_with_currency_format":"{{amount}} NIS"
},
"JMD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} JMD"
},
"JPY":{
"money_format":"&#165;{{amount_no_decimals}}",
"money_with_currency_format":"&#165;{{amount_no_decimals}} JPY"
},
"JEP":{
"money_format":"&pound;{{amount}}",
"money_with_currency_format":"&pound;{{amount}} JEP"
},
"JOD":{
"money_format":"{{amount}}0 JD",
"money_with_currency_format":"{{amount}}0 JOD"
},
"KZT":{
"money_format":"{{amount}} KZT",
"money_with_currency_format":"{{amount}} KZT"
},
"KES":{
"money_format":"KSh{{amount}}",
"money_with_currency_format":"KSh{{amount}}"
},
"KWD":{
"money_format":"{{amount}}0 KD",
"money_with_currency_format":"{{amount}}0 KWD"
},
"KGS":{
"money_format":"лв{{amount}}",
"money_with_currency_format":"лв{{amount}}"
},
"LVL":{
"money_format":"Ls {{amount}}",
"money_with_currency_format":"Ls {{amount}} LVL"
},
"LBP":{
"money_format":"L&pound;{{amount}}",
"money_with_currency_format":"L&pound;{{amount}} LBP"
},
"LTL":{
"money_format":"{{amount}} Lt",
"money_with_currency_format":"{{amount}} Lt"
},
"MGA":{
"money_format":"Ar {{amount}}",
"money_with_currency_format":"Ar {{amount}} MGA"
},
"MKD":{
"money_format":"ден {{amount}}",
"money_with_currency_format":"ден {{amount}} MKD"
},
"MOP":{
"money_format":"MOP${{amount}}",
"money_with_currency_format":"MOP${{amount}}"
},
"MVR":{
"money_format":"Rf{{amount}}",
"money_with_currency_format":"Rf{{amount}} MRf"
},
"MXN":{
"money_format":"$ {{amount}}",
"money_with_currency_format":"$ {{amount}} MXN"
},
"MYR":{
"money_format":"RM{{amount}} MYR",
"money_with_currency_format":"RM{{amount}} MYR"
},
"MUR":{
"money_format":"Rs {{amount}}",
"money_with_currency_format":"Rs {{amount}} MUR"
},
"MDL":{
"money_format":"{{amount}} MDL",
"money_with_currency_format":"{{amount}} MDL"
},
"MAD":{
"money_format":"{{amount}} dh",
"money_with_currency_format":"Dh {{amount}} MAD"
},
"MNT":{
"money_format":"{{amount_no_decimals}} &#8366",
"money_with_currency_format":"{{amount_no_decimals}} MNT"
},
"MZN":{
"money_format":"{{amount}} Mt",
"money_with_currency_format":"Mt {{amount}} MZN"
},
"NAD":{
"money_format":"N${{amount}}",
"money_with_currency_format":"N${{amount}} NAD"
},
"NPR":{
"money_format":"Rs{{amount}}",
"money_with_currency_format":"Rs{{amount}} NPR"
},
"ANG":{
"money_format":"&fnof;{{amount}}",
"money_with_currency_format":"{{amount}} NA&fnof;"
},
"NZD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} NZD"
},
"NIO":{
"money_format":"C${{amount}}",
"money_with_currency_format":"C${{amount}} NIO"
},
"NGN":{
"money_format":"&#8358;{{amount}}",
"money_with_currency_format":"&#8358;{{amount}} NGN"
},
"NOK":{
"money_format":"kr {{amount_with_comma_separator}}",
"money_with_currency_format":"kr {{amount_with_comma_separator}} NOK"
},
"OMR":{
"money_format":"{{amount_with_comma_separator}} OMR",
"money_with_currency_format":"{{amount_with_comma_separator}} OMR"
},
"PKR":{
"money_format":"Rs.{{amount}}",
"money_with_currency_format":"Rs.{{amount}} PKR"
},
"PGK":{
"money_format":"K {{amount}}",
"money_with_currency_format":"K {{amount}} PGK"
},
"PYG":{
"money_format":"Gs. {{amount_no_decimals_with_comma_separator}}",
"money_with_currency_format":"Gs. {{amount_no_decimals_with_comma_separator}} PYG"
},
"PEN":{
"money_format":"S/. {{amount}}",
"money_with_currency_format":"S/. {{amount}} PEN"
},
"PHP":{
"money_format":"&#8369;{{amount}}",
"money_with_currency_format":"&#8369;{{amount}} PHP"
},
"PLN":{
"money_format":"{{amount_with_comma_separator}} zl",
"money_with_currency_format":"{{amount_with_comma_separator}} zl PLN"
},
"QAR":{
"money_format":"QAR {{amount_with_comma_separator}}",
"money_with_currency_format":"QAR {{amount_with_comma_separator}}"
},
"RON":{
"money_format":"{{amount_with_comma_separator}} lei",
"money_with_currency_format":"{{amount_with_comma_separator}} lei RON"
},
"RUB":{
"money_format":"&#1088;&#1091;&#1073;{{amount_with_comma_separator}}",
"money_with_currency_format":"&#1088;&#1091;&#1073;{{amount_with_comma_separator}} RUB"
},
"RWF":{
"money_format":"{{amount_no_decimals}} RF",
"money_with_currency_format":"{{amount_no_decimals}} RWF"
},
"WST":{
"money_format":"WS$ {{amount}}",
"money_with_currency_format":"WS$ {{amount}} WST"
},
"SAR":{
"money_format":"{{amount}} SR",
"money_with_currency_format":"{{amount}} SAR"
},
"STD":{
"money_format":"Db {{amount}}",
"money_with_currency_format":"Db {{amount}} STD"
},
"RSD":{
"money_format":"{{amount}} RSD",
"money_with_currency_format":"{{amount}} RSD"
},
"SCR":{
"money_format":"Rs {{amount}}",
"money_with_currency_format":"Rs {{amount}} SCR"
},
"SGD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} SGD"
},
"SYP":{
"money_format":"S&pound;{{amount}}",
"money_with_currency_format":"S&pound;{{amount}} SYP"
},
"ZAR":{
"money_format":"R {{amount}}",
"money_with_currency_format":"R {{amount}} ZAR"
},
"KRW":{
"money_format":"&#8361;{{amount_no_decimals}}",
"money_with_currency_format":"&#8361;{{amount_no_decimals}} KRW"
},
"LKR":{
"money_format":"Rs {{amount}}",
"money_with_currency_format":"Rs {{amount}} LKR"
},
"SEK":{
"money_format":"{{amount_no_decimals}} kr",
"money_with_currency_format":"{{amount_no_decimals}} kr SEK"
},
"CHF":{
"money_format":"SFr. {{amount}}",
"money_with_currency_format":"SFr. {{amount}} CHF"
},
"TWD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} TWD"
},
"THB":{
"money_format":"{{amount}} &#xe3f;",
"money_with_currency_format":"{{amount}} &#xe3f; THB"
},
"TZS":{
"money_format":"{{amount}} TZS",
"money_with_currency_format":"{{amount}} TZS"
},
"TTD":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}} TTD"
},
"TND":{
"money_format":"{{amount}}",
"money_with_currency_format":"{{amount}} DT"
},
"TRY":{
"money_format":"{{amount}}TL",
"money_with_currency_format":"{{amount}}TL"
},
"UGX":{
"money_format":"Ush {{amount_no_decimals}}",
"money_with_currency_format":"Ush {{amount_no_decimals}} UGX"
},
"UAH":{
"money_format":"₴{{amount}}",
"money_with_currency_format":"₴{{amount}} UAH"
},
"AED":{
"money_format":"Dhs. {{amount}}",
"money_with_currency_format":"Dhs. {{amount}} AED"
},
"UYU":{
"money_format":"${{amount_with_comma_separator}}",
"money_with_currency_format":"${{amount_with_comma_separator}} UYU"
},
"VUV":{
"money_format":"${{amount}}",
"money_with_currency_format":"${{amount}}VT"
},
"VEF":{
"money_format":"Bs. {{amount_with_comma_separator}}",
"money_with_currency_format":"Bs. {{amount_with_comma_separator}} VEF"
},
"VND":{
"money_format":"{{amount_no_decimals_with_comma_separator}}&#8363;",
"money_with_currency_format":"{{amount_no_decimals_with_comma_separator}} VND"
},
"XBT":{
"money_format":"{{amount_no_decimals}} BTC",
"money_with_currency_format":"{{amount_no_decimals}} BTC"
},
"XOF":{
"money_format":"CFA{{amount}}",
"money_with_currency_format":"CFA{{amount}} XOF"
},
"ZMW":{
"money_format":"K{{amount_no_decimals_with_comma_separator}}",
"money_with_currency_format":"ZMW{{amount_no_decimals_with_comma_separator}}"
}
};
Currency.currentCurrency = '';
Currency.format = 'money_with_currency_format';
Currency.convertAll = function(oldCurrency, newCurrency, selector, format) {
// If oldCurrency, newCurrency, or selector are not defined, set default values
oldCurrency = oldCurrency || 'USD';
newCurrency = newCurrency || 'USD';
selector = selector || 'span.money';
// Get all matching elements
var elements = document.querySelectorAll(selector);
// Iterate over each element
elements.forEach(element => {
// If the amount has already been converted, skip it
if (element.dataset.currency === newCurrency) {
return;
}
// If the element has a saved value for the new currency, use it
if (element.dataset['currency' + newCurrency]) {
element.innerHTML = element.dataset['currency' + newCurrency];
}
else {
// Convert the amount to cents
var cents = parseInt(element.innerHTML.replace(/[^0-9]/g, ''), 10);
var oldFormat = Currency.moneyFormats[oldCurrency][format || Currency.format] || '{{amount}}';
var newFormat = Currency.moneyFormats[newCurrency][format || Currency.format] || '{{amount}}';
if (oldFormat.indexOf('amount_no_decimals') !== -1) {
cents *= 100;
}
else if (oldCurrency === 'JOD' || oldCurrency === 'KWD' || oldCurrency === 'BHD') {
cents /= 10;
}
// Convert the amount to the new currency and format it
var newFormattedAmount = Shopify.formatMoney(Currency.convert(cents, oldCurrency, newCurrency), newFormat);
element.innerHTML = newFormattedAmount;
element.dataset['currency' + newCurrency] = newFormattedAmount;
}
// Mark the element as converted to the new currency
element.dataset.currency = newCurrency;
})
// Save the current currency and write a cookie
this.currentCurrency = newCurrency;
if (this.cookie) {
this.cookie.write(newCurrency);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment