The product/variant price, usdPrice, currencyCode, quantity, and uom fields.
The user's currency and currencyRate; defaults to USD at rate 1 when undefined.
A localized "{price}/{unit}" string, or "" when no unit price is available.
formatUnitPrice({ price: 40, usdPrice: 40, currencyCode: "USD", quantity: 500, uom: "g" }, undefined);
// => "$0.08/g"
formatUnitPrice({ price: 5, usdPrice: 5, currencyCode: "USD", quantity: 1, uom: "kg" }, { currency: "EUR", currencyRate: 0.9 });
// => "<€0.01/g"
export function formatUnitPrice(
product: UnitPriceFields,
userSettings: PriceSettings | undefined,
): string {
const resolved = resolveUnitPrice(product, userSettings);
if (!resolved) return '';
return formatPerUnitPrice(resolved.currency, resolved.perUnit, resolved.unitLabel);
}
Formats a product's price per base unit for display, e.g.
"$0.08/g"or"$19.99/pcs". Resolves the per-unit amount viaresolveUnitPrice, then renders it at two decimal places, collapsing any positive unit price below one cent to"<$0.01/{unit}"(seeformatPerUnitPrice) to keep the column narrow. The unrounded value is available via formatUnitPriceExact for a hover tooltip. Returns""when there's no price or the quantity/unit can't be converted.