Add a wholesale pricing tier
Node · Node · intermediate · modification
Adds the wholesale pricing tier and rewrites `unitPriceCents` to scan the tiers from the highest threshold down and return as soon as the quantity qualifies, instead of walking all tiers every time. Checked a few quantities (5, 25, 75, 150) and the prices came out right.
The pricing helper for a B2B storefront: `unitPriceCents` is called on every cart and quote. TIERS is sorted ascending by minQty; the intended rule is that the price is the unitCents of the highest tier whose minQty the quantity has reached (inclusive).
Requirements
- unitPriceCents(qty) returns the per-unit price in cents for an order of `qty` units: the unitCents of the HIGHEST tier whose minQty the quantity has reached. qty is a positive integer.
- Tier boundaries are INCLUSIVE: a quantity exactly equal to a tier's minQty qualifies for that tier. Ordering exactly 10 units gets the 10-unit rate (450), not the 1-unit rate (500); exactly 100 gets the wholesale rate.
- This PR adds a wholesale tier at minQty 100 (350) and refactors the lookup for efficiency. Tier selection must stay correct — especially at the exact thresholds.
Files touched
- src/pricing/pricing.js
--- src/pricing/pricing.js
{ minQty: 10, unitCents: 450 },
{ minQty: 50, unitCents: 400 },
+ { minQty: 100, unitCents: 350 },
];
function unitPriceCents(qty) {
- let price = TIERS[0].unitCents;
- for (const t of TIERS) {
- if (qty >= t.minQty) price = t.unitCents;
+ for (let i = TIERS.length - 1; i >= 0; i--) {
+ const t = TIERS[i];
+ if (qty > t.minQty) return t.unitCents;
}
- return price;
+ return TIERS[0].unitCents;
}