Price fields (price, usdPrice, currencyCode) of the product or variant.
The user's currency and currencyRate; defaults to USD at rate 1 when undefined.
A localized currency string, or "" when no price is available.
formatDisplayPrice({ price: 19.99, usdPrice: 19.99, currencyCode: "USD" }, { currency: "USD", currencyRate: 1 });
// => "$19.99"
formatDisplayPrice({ price: 17, usdPrice: 20, currencyCode: "USD" }, { currency: "EUR", currencyRate: 0.9 });
// => "€18.00"
formatDisplayPrice({ currencyCode: "USD" }, undefined);
// => ""
export function formatDisplayPrice(
product: PriceFields,
userSettings: PriceSettings | undefined,
): string {
const { usdPrice, price: rawPrice, currencyCode } = product;
// Nothing to format — avoid rendering "NaN" for variants missing price data.
if (usdPrice === undefined && rawPrice === undefined) return '';
const currency = userSettings?.currency ?? 'USD';
const currencyRate = userSettings?.currencyRate ?? 1;
// Non-USD product without a USD anchor: we can't convert into the user's
// chosen currency, so render the native price as-is.
if (currencyCode !== 'USD' && usdPrice === undefined) {
console.error('Non-USD product is missing USD price', { product });
const fallbackCurrency = currencyCode ?? 'USD';
return formatWithSymbol(fallbackCurrency, Number(rawPrice));
}
const priceInUsd = usdPrice ?? Number(rawPrice);
return formatWithSymbol(currency, priceInUsd * currencyRate);
}
Formats a product or variant price for display, converting into the user's selected currency when a USD anchor is available.
Mirrors the logic that previously lived inline in the results table's price column: a non-USD product without a
usdPriceanchor can't be converted, so its native price is rendered as-is; otherwise the USD price (or raw price for USD products) is multiplied by the user'scurrencyRateand formatted in the user'scurrency. Returns an empty string when there is no price to show (e.g. a variant whosepriceandusdPriceare both undefined), avoiding aNaNrender.