A shop adds a coupon. Later it adds bundles. Then a sale on one product. Each change is small, and each is built where it was needed: a coupon check in the cart, a bundle price on the product page, a discount column in the invoice code. Six months later the product page shows one price, the cart another, the invoice a third, and support is explaining to a customer why the number changed.
The cause is almost never a bug in one place. It is that the question "what does this cost for this person, right now?" is answered in several places, each with slightly different rules. This article is about answering it in exactly one.
Rule 1: one service owns the price
Build one piece of code whose whole job is to answer that question. It takes the item, the customer and whatever they have applied (a coupon, a bundle), and returns a quote: the original price, the discount, where the discount came from, and the final price.
Everything else asks it. The product page, the cart, the checkout API, the invoice and the reports all call the same function. The interface displays the quote and never calculates one. When a rule changes, it changes in one file, and every screen agrees the next minute.
Rule 2: choose a stacking policy on purpose
When a coupon, a bundle discount and an item discount all apply, something has to decide what happens. There are four common policies, and each is a business decision:
| Policy | What it means | Good for | Watch out for |
|---|---|---|---|
| Fixed priority | The first that applies wins, in an order you define (say coupon, then bundle, then item) | Predictable prices, easy to explain | A coupon can beat a better bundle discount |
| Best for the customer | Compute each, apply the largest | Simple promise to customers | Margins you did not plan for |
| Stack in order | Apply one after another on the running price | Loyalty and campaigns | Totals nobody can predict by hand |
| Never stack | At most one discount, ever | Tight margins | Customers feel the system is arbitrary unless the rule is stated |
Here is the same basket under three of them. The price is EGP 1,000; the coupon is 10% capped at EGP 80; the bundle is 15%; the item has a fixed EGP 200 discount.
| Policy | Calculation | Final price |
|---|---|---|
| Fixed priority: coupon, bundle, item | coupon: 80 | EGP 920 |
| Best for the customer | the largest of 80, 150, 200 | EGP 800 |
| Stack in order | 1,000 - 80 = 920; bundle 15% of 920 = 138; 782 - 200 | EGP 582 |
Same basket, three prices, all defensible. What is not defensible is not knowing which one your system uses. Write the policy down in a sentence, and put it at the top of the code that implements it.
Rule 3: give every discount an explicit rulebook
A discount that "just works" is a discount with unwritten rules. For each kind, list its rules and make the code say them in the same words.
A coupon
- type: a percentage or a fixed amount
- a cap on the discount (a percentage with no cap is a surprise on a large basket)
- a minimum purchase
- a start and an end, with the time zone stated
- a limit in total, and a limit per customer
- who may use it: everyone, or named customers
- an on and off switch
A bundle
- which items it holds, and whether the customer must buy them all
- a percentage or a fixed discount
- a validity window
- what happens when the customer already owns one of the items
Then add two rules that apply to everything: a discount never exceeds the price, and the final price is never below zero.
Rule 4: money is integers, and rounding happens once
Do the arithmetic in the smallest unit of the currency (piasters, not pounds) as integers, or with a decimal type. Floating-point numbers turn 0.1 + 0.2 into 0.30000000000000004, and a total that is a fraction of a pound off is the kind of bug accountants find.
Decide where rounding happens and do it once per step, in a documented direction. Percentages are computed on a stated base (the original price, or the running price), and the base belongs in the policy too.
Rule 5: save the breakdown with the order
Prices change. Coupons expire. Bundles are edited. If an invoice or a refund recomputes the price from today's rules, last month's order shows a different total.
When an order is placed, store the quote with it: the original price, the source of the discount, its amount, the coupon or bundle id, and the final price. Refunds, invoices and reports read the stored quote. The engine is for the next purchase, and the order remembers the last one.
Rule 6: limits are race conditions
"One use per customer" and "100 uses in total" fail in the same way as stock. Two checkouts read "99 used" at once and both succeed.
Two defences, used together: a unique index on (coupon_id, customer_id) for per-customer limits, and the counter incremented inside the order's transaction, with the coupon row locked while it is checked.
A small version in code
This sketch uses a fixed priority. To switch to "best for the customer", replace the first candidate with the one that has the largest discount.
final readonly class Quote
{
public function __construct(
public int $original, // in piasters
public string $source, // coupon | bundle | item | none
public int $discount,
public int $final,
) {}
}
function quote(int $price, ?Coupon $coupon, ?Bundle $bundle, int $itemDiscount): Quote
{
$candidates = [];
if ($coupon?->isUsable()) {
$candidates[] = ['coupon', $coupon->discountFor($price)];
}
if ($bundle?->isActive()) {
$candidates[] = ['bundle', $bundle->discountFor($price)];
}
if ($itemDiscount > 0) {
$candidates[] = ['item', $itemDiscount];
}
[$source, $discount] = $candidates[0] ?? ['none', 0];
$discount = min($discount, $price);
return new Quote($price, $source, $discount, $price - $discount);
}
The Quote carries its source, so the interface can tell the customer "a coupon was applied instead of the bundle discount" instead of leaving them to wonder.
Test the table, not the code
Price rules are a table of inputs and outputs, so test them that way. A dozen rows catch most of the damage:
- no discount at all
- a percentage coupon below its cap, and above it
- a fixed coupon larger than the price
- a coupon one second before it starts, and one second after it ends
- a coupon used up by someone else a moment ago
- a bundle with one item already owned
- two discounts together, under your policy
- a price of zero
- rounding on an odd amount, such as 33.33% of 100.01
When a rule changes, one row changes and the rest keep passing.
A checklist
- One service answers every price question, and nothing else calculates.
- The stacking policy is written in a sentence and applied in code.
- Each discount type has a written rulebook with caps, windows and limits.
- Money is integers or decimals, and rounding is decided once.
- The quote is stored with the order.
- Limits are enforced with a unique index and a transaction.
- The customer can see which discount was applied.
- A table of cases tests the rules.
If you are building a shop, a booking platform or a learning platform with coupons, bundles and promotions, and want the pricing rules reviewed before they spread through the code, talk to us.




