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

Files touched

--- 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;
 }
 

Review this PR

Node practice