@zenuinebit/pricing-rules
v0.1.0
Published
Promotions for a cart: buy X get Y, quantity tiers, bundles, percent off and spend thresholds.
Readme
@zenuinebit/pricing-rules
Promotions for a @zenuinebit/cart: buy X get Y, quantity tiers, bundles, percent off and spend thresholds. Amounts are integers in minor units.
import { createCart, addItem, computeTotals } from "@zenuinebit/cart";
import { applyPromotions } from "@zenuinebit/pricing-rules";
let cart = createCart({ currency: "INR" });
cart = addItem(cart, { sku: "TEA", name: "Tea 250g", unitPrice: 12000, quantity: 3 });
const { cart: priced, applied } = applyPromotions(cart, [
{ id: "tea-b2g1", name: "Buy 2 get 1 free", type: "buy-x-get-y", sku: "TEA", buy: 2, get: 1 },
{ id: "big-basket", name: "10% off above 500", type: "spend-threshold", minSpend: 50000, percent: 10 },
]);
applied; // [{ ruleId, name, amount, skus }] - show these on the receipt
computeTotals(priced); // normal cart totals, with the promotions includedRules
| type | What it does |
|---|---|
| buy-x-get-y | Every buy + get units of a SKU, get are free |
| quantity-tier | tiers: [{minQty, percent}], the highest tier reached applies to the line |
| percent-off | Percent off listed skus, or every line if omitted |
| bundle | items sold together for price; each complete set is discounted, and the saving is split exactly across lines |
| spend-threshold | Cart-level percent or amount off once the cart (after line promotions) reaches minSpend |
How rules combine
- Line rules run in the order given and a line gets at most one promotion: the first rule that discounts it wins. Put your best deals first.
- Lines that already have a manual discount are left alone.
- Spend-threshold rules run last, on what is left after line promotions.
- Calling
applyPromotionsagain starts from scratch, so it is safe to re-run after the cart changes. The input cart is never changed. - Promotions are stored as discounts with a
promo:<id>code, so they show up in the normal cart totals.
Not included: coupon codes and usage limits, date ranges, customer groups, and stacking several promotions on one line. Check those in your app before passing the rules in.
