How Gatemi Discount Rules Works

Three rules shape this plugin. Each exists because discount plugins get it wrong in a way a merchant only notices at the end of the month.

Rule 1: one calculator answers every page

The shop page, the product page and the cart all ask the same code for a product's price. A product cannot be listed at one price and charged at another, because there is no second place where the number is worked out.

In the free edition that code is the quantity-break lookup for the product. In the full edition it is the rule engine: one list of conditions, one evaluator deciding whether a rule applies, one calculator producing every price the plugin shows or charges.

Rule 2: every cart line remembers the price it came in at

WooCommerce recalculates a cart several times on one page load and again on every cart update, and the product on a cart line is a live object whose price this plugin changes. If each pass measured from the price it found, 10% off would become 10% off 10% off on the next pass, and so on — prices that drift downwards slowly enough to go unnoticed.

So the first time a line is priced, its price is stored on the line (gmdrf_base in the free edition, gmdr_base in the full edition), and every pass measures from that. Recalculate as often as you like; the price does not move. When a line stops qualifying — the quantity drops below the break — it goes back to the stored price.

Lines are repriced on woocommerce_before_calculate_totals at priority 20. That is the one hook the classic cart, the mini-cart, the Cart and Checkout blocks and the Store API all recalculate through, so there is no separate code for each.

Rule 3: quantities pool across lines

A shopper who adds 3 of a product, then 3 more from another page, has 6. Breaks count every line of the product together. In the full edition, a quantity-break rule counts every cart line its scope covers — so a break scoped to a category counts the whole category.

Pricing a line — free edition

  1. Add up the cart quantity of the product across all its lines — for a variation, of its parent product, so every variation counts together.
  2. Find the highest break that quantity has reached.
  3. Take the break off the stored price: a percentage (at most 100) or an amount per unit. Never below 0, rounded to the number of decimals the store prices in (WooCommerce → Settings → General).
  4. If that is lower than the stored price, set it and put a badge on the line ("10% off").

Worked example: price £10, breaks at 5 (10% off) and 10 (£2 off). A cart of 4 pays £10 each. At 5 it pays £9 each. At 12 it pays £8 each.

On the product page, the price gets a "5+ from £9.00 each" note and the Buy more, pay less table lists each break.

Pricing a line — Full edition

The order rules run in

Rules run top to bottom, in the order on the screen. For one cart line:

  1. Skip cart and buy-one-get-one rules — they are not line prices.
  2. Skip a rule that is not active, does not cover this product, or has a condition that does not hold.
  3. Work out the price the rule gives, always from the stored price, never from what an earlier rule gave.
  4. Keep the lowest price found so far.
  5. If the rule does not have Keep looking at later rules ticked, stop.

With the default — every rule stops — the first matching rule decides. If several matching rules are allowed to carry on, the cheapest one wins; discounts never stack on top of each other.

Quantity breaks in the full edition

A tiered rule counts the cart quantity of everything its scope covers. On the catalog page the price shown is the price at the shopper's current cart, which is the truthful answer; the tier table under the product shows what a bigger cart would cost. The table ignores conditions about how much is in the cart now (subtotal and item counts) but keeps every other condition — dates, roles, products present.

Whole-cart discounts

A Discount the whole cart rule is added as a negative fee, named by its badge or its title. A percentage is of the cart subtotal at the stored prices. The discount is capped at what the cart is worth, so £50 off a £30 cart takes off £30 rather than paying the customer £20. Fees survive tax and shipping recalculation and are shown natively by the classic cart, the blocks and every email.

Buy one, get one

  1. Every unit of every line the rule covers is listed, with its stored price.
  2. The list is sorted cheapest first.
  3. For every full set of Buy + Get units, Get units are free — and they are the cheapest units in the cart. Reordering the cart cannot change which ones.
  4. The total is taken off as a negative fee.

Example: Buy 2 Get 1, a cart of mugs at £8, £10 and £12 and a £15 plate, all covered. Four units make one set of three, so one unit is free: the £8 mug.

What lands on the order

Each discounted line keeps _gmdr_rule (the rule id), _gmdr_base (the price before the discount) and _gmdr_label (the badge) as hidden order item meta, so a past order still says why it was cheaper.

When both editions are active

The free edition checks for the full edition on load and registers nothing if it is active. The full edition reads the free edition's breaks as ordinary rules, so nothing needs re-entering.

Shopping Cart
Scroll to Top