Skip to content

Instantly share code, notes, and snippets.

@panoply
Created June 21, 2018 07:28
Show Gist options
  • Save panoply/8d207e8d759783432554fef04539b232 to your computer and use it in GitHub Desktop.
Save panoply/8d207e8d759783432554fef04539b232 to your computer and use it in GitHub Desktop.
Shopify Currency Exchange

Currency Exchange for Shopify

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.

/**
* 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