A simple ES6 Implementation for Shopify multiple currency. Required you to use
<span class=money data-base={{amount}}>${{amount}}
in you config settings in Shopify.
Created
June 21, 2018 07:28
-
-
Save panoply/8d207e8d759783432554fef04539b232 to your computer and use it in GitHub Desktop.
Shopify Currency Exchange
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
/** | |
* Currency Converter | |
* Used by Locale component for currency exchange rates. | |
*/ | |
/* Modules */ | |
import fx from "money"; | |
import accounting from "accounting"; | |
import symbol from 'currency-symbol-map'; | |
import store from 'store/dist/store.modern'; | |
export default class Money { | |
constructor(base){ | |
this.base = base; | |
this.code = store.get('locale').currency; | |
this.prices = document.querySelectorAll('span.money'); | |
this.codes = document.querySelectorAll('span.codes'); | |
this.convert(); | |
} | |
convert(){ | |
fx.rates = Currency.rates; | |
this.prices.forEach(price => this.conversion(price)); | |
this.codes.forEach(code => code.innerHTML = this.code); | |
} | |
conversion(price) { | |
const value = accounting.unformat(price.dataset.base); | |
const convert = fx(value).from(this.code).to(config.locale.currency.base); | |
const round = accounting.toFixed(convert, 0); | |
const sign = symbol(this.code); | |
const amount = accounting.formatMoney(round, { | |
symbol: this.code, | |
format: "%v %s", | |
precision: 2, | |
thousand: ',' | |
}); | |
const format = sign === this.code ? `${amount}` : `${sign} ${amount}`; | |
if(!this.base) { | |
price.innerHTML = format; | |
} else { | |
price.innerHTML = price.dataset.base | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment