I'm retrieving API data that has a price listed in USD. The USD price is being shown on a card in a search listing page.
I want to instead replace the USD price with it's Canadian price. I have a 'formatPrice' function where part of that function converts USD to CAD and it works in a separate area of the web app. Here's the code:
export function formatPrice(
price,
lang,
inclCurTxt?: boolean,
currency?: string,
receivedCurrency?: string
) {
let formattedPrice = price;
let usaRate = 0.74;
//ENGLISH
const currencyText = inclCurTxt ? " CAD" : "";
if (currency != null && currency === "US") {
//USD
formattedPrice =
accounting.formatMoney(Number(price) * usaRate, "$", 0) + currencyText;
} else {
//CAD
formattedPrice = accounting.formatMoney(price, "$", 0) + currencyText;
}
// Alter conversions for BFE (English) on single product pages
if (isEnvironmentBFE()) {
if (currency !== receivedCurrency) {
formattedPrice =
receivedCurrency === "CAD"
? accounting.formatMoney(Number(price) * usaRate, "$", 0) +
currencyText
: accounting.formatMoney(Number(price) / usaRate, "$", 0) +
currencyText;
} else {
formattedPrice =
accounting.formatMoney(Number(price), "$", 0) + currencyText;
// this shows up on ALL prices (cdn or usd) on the listings page, and ONLY
// on American prices on the single products.
}
}
return formattedPrice;
}
I have curPrice which basically uses formatPrice, and it currently shows the USD price on a card in a search listing page. This code works great already - I just want to add onto it. Here's the code:
let curPrice =
this.props.lang === "en"
? "Call for price"
: "Contactez-nous pour le prix";
if (curProduct.price != null) {
curPrice = formatPrice(
curProduct.price.text,
this.props.lang,
true
);
}
I've written some code that if current price being pulled into the app from the API is in USD, retrieve the Canadian price from the formatPrice function above and make that appear instead. Here's the code however it's not working:
if (curProduct.price?.currency !== "CAD") {
curPrice = formatPrice(
curProduct.price.text * 0.74,
this.props.lang,
true
);
}