/** * A variant's sale price only counts inside its sale window * (`salePriceStartsAt` <= now < `salePriceEndsAt`; a missing date is open on * that side). Outside the window the cart charges the regular price, so the * page must not show the sale price then either. * * The product-level `salePrice` is already cleared by the API outside its * window; a variant's raw `salePrice` is not, which is why every place that * reads `variant.salePrice` goes through this. * * Same rule as `isSalePriceActive` in the brainerce SDK (kept local so this * starter does not depend on a newer SDK release). */ export function liveVariantSalePrice( variant: | { salePrice?: string | null; salePriceStartsAt?: string | null; salePriceEndsAt?: string | null; } | null | undefined, now: Date = new Date() ): string | null { if (!variant?.salePrice) return null; const t = now.getTime(); const start = variant.salePriceStartsAt ? Date.parse(variant.salePriceStartsAt) : NaN; if (Number.isFinite(start) && t < start) return null; const end = variant.salePriceEndsAt ? Date.parse(variant.salePriceEndsAt) : NaN; if (Number.isFinite(end) && t >= end) return null; return variant.salePrice; }