Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vwo-kb/f938a3416f4b776cb480e0e0570879ba to your computer and use it in GitHub Desktop.
Save vwo-kb/f938a3416f4b776cb480e0e0570879ba to your computer and use it in GitHub Desktop.
function convertToDefaultCurrency(amount, fromCurrency, precision = 2) {
/**
* Your array of currencies and their conversion rates relative to the default currency
* Example: If USD is your store currency or the one in which you want to track revenue and
* 1 USD = 0.8 GBP then the entry in the array will be "GBP": 0.8
**/
var currencyConversion = {
"USD": 1,// Always keep the default store currency in the array and keep the value as 1
"EUR": 1.1,
"GBP": 0.82,
"AUD": 1.43,
"CAD": 1.4,
"CZK": 26,
"DKK": 7.3,
"HKD": 7.76,
"ISK": 170,
"ILS": 3.8,
"NZD": 1.43,
"NOK": 14,
"PLN": 4.6,
"RON": 5,
"SGD": 1.34,
"KRW": 1200,
"SEK": 10.55,
"JPY": 120,
"INR": 83.52,
};
if (!currencyConversion[fromCurrency.toUpperCase()]) {
console.error("Unsupported currency code: " + fromCurrency.toUpperCase());
// Handling can be added for additional currencies
return null;
}
return parseFloat((amount / currencyConversion[fromCurrency.toUpperCase()]).toFixed(precision));
}
vwo.addShopifyMiddleware((payload) => {
var newPayload = payload;
if(payload.name === "shopify.purchase"){
newPayload.props.totalPrice = convertToDefaultCurrency(newPayload.props.totalPrice, newPayload.props.currencyCode, 2);
}
console.log(newPayload);
return newPayload;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment