Skip to content

docs(specs): remove reward lines the cart no longer earns - #2937

Merged
glennjacobs merged 1 commit into
2.xfrom
docs/spec-remove-unearned-reward-lines
Nov 27, 2026
Merged

glennjacobs merged 1 commit into
2.xfrom
docs/spec-remove-unearned-reward-lines

Conversation

@glennjacobs

Copy link
Copy Markdown
Contributor

New spec, remove-unearned-reward-lines. It follows up on core-values-out-of-meta (#2861): a line that BuyXGetY adds automatically stays in the cart, and is charged at full price, once the cart stops earning it.

What happens on 2.x today (each case was run against 8ab3f9da; "buy A get B free", both £10 with 20% tax)

  • Remove A: the B line stays and the cart totals £12.
  • The discount's availability ends: B is charged at full price, £24 in total.
  • A goes from 2 down to 1 after B was added at quantity 2: one B is charged, £24.
  • A goes up to 3: three B are earned but only one is given.
  • The shopper sets the added B line to 3: two units are charged.
  • An added line counts toward its own discount's conditions and minimum spend, so a collection condition or a minimum spend can keep the reward earned by itself.
  • $cart->freeItems gains an entry on every recalculation of the same instance.
  • Deleting the discount nulls added_by_discount_id, so its lines become ordinary shopper lines at full price.

The proposal

  • A line a discount added holds exactly the reward units that discount allocates to it in the current calculation.
  • BuyXGetY only allocates: it keeps existing added lines first, then raises them, then adds lines. It no longer saves or prices lines.
  • A new ReconcilesRewardLines action creates, resizes or deletes added lines from the discount breakdown. CalculateCart runs the calculation steps once more when the action changed anything, so shipping and tax see the corrected lines.
  • Added lines no longer count toward any discount's conditions, minimum spend or discounted lines. That is what lets the second pass settle in one step.
  • Deleting a discount removes its added lines from carts without a completed order.
  • A new CartLineAddedByDiscount validator refuses quantity changes to an added line (one new string in 16 locales).
  • No schema change.

Behaviour changes to review

  • PercentageOff and FixedAmountOff no longer see added lines, so a free gift no longer counts toward any minimum spend.
  • Turning off automatically_add_rewards removes the existing added lines.

Out of scope: remembering that a shopper declined a gift. A removed gift still comes back while the cart earns it, as it does today.

@github-project-automation github-project-automation Bot moved this to Todo in Roadmap Nov 27, 2026
@glennjacobs
glennjacobs merged commit 582b124 into 2.x Nov 27, 2026
6 checks passed
@glennjacobs
glennjacobs deleted the docs/spec-remove-unearned-reward-lines branch November 27, 2026 13:48
@github-project-automation github-project-automation Bot moved this from Todo to Done in Roadmap Nov 27, 2026
glennjacobs added a commit that referenced this pull request Nov 27, 2026
…ward-lines) (#2938)

Implements spec `remove-unearned-reward-lines` (#2937). One commit per
slice.

A line a discount added now always holds exactly the reward units that
discount earns in the current calculation. Before this, an automatically
added Buy X Get Y reward stayed in the cart, and was charged at full
price, once the cart stopped earning it.

## Slice 1: reconciliation
- `BuyXGetY` allocates the units its shopper lines can't take onto its
own lines (keep, then raise, then add) and no longer saves or resizes
lines.
- New `ReconcilesRewardLines` action, bound in `ActionServiceProvider`.
It creates, resizes or deletes every line with `added_by_discount_id` to
match the discount breakdown, so a discount that no longer applies loses
its lines.
- `CalculateCart` runs the `CartCalculation` steps a second time only
when reconciling changed a line, so shipping and tax see the corrected
lines.
- Added lines no longer count toward any discount's conditions, minimum
spend or targeted lines (`BuyXGetY`,
`AbstractDiscountType::getEligibleLines()`, `TargetsCartLines`). A
reward can no longer keep itself earned.
- `ApplyDiscounts` resets `freeItems`, so recalculating the same
instance no longer repeats entries.

## Slice 2: deleting a discount
`DiscountObserver::deleting()` deletes the discount's added lines on
carts without a completed order, one model at a time so each cart's
persisted totals go stale. Carts with a completed order keep their
lines.

## Slice 3: refusing quantity changes
New `CartLineAddedByDiscount` validator in `UpdateCartLineValidation`.
It refuses a quantity change on an added line and accepts a meta-only
update at the same quantity. New `carts.line_added_by_discount` message
in all 16 locales. The spec said "promotion"; the message says
"discount", matching every locale's existing terminology, and the spec
is updated.

## Slice 4: docs
`reference/discounts.mdx`, `reference/carts.mdx`,
`extending/discounts.mdx` (how a custom type has core manage a line it
adds), `extending/carts.mdx` (the second pass), `guides/cart.mdx`
(render added lines without quantity controls) and a behaviour note in
the alpha upgrade guide. No schema change.

## Behaviour changes to review
- `PercentageOff` and `FixedAmountOff` no longer see added lines.
- Turning off `automatically_add_rewards` removes existing added lines.
- A calculation that changes an added line runs the calculation steps
twice.

## Existing tests changed
- `BuyXGetYTest`: when the shopper already has the reward product and
the cart earns 3, an added line now holds the 2 units their line can't
take (before, they were never given).
- `CalculateCartTest`: the pipeline runs twice on the calculation that
adds the reward line.
- `GetExistingCartLineTest` and `MapDiscountBreakdownTest` built an
"added" line from a discount that never applies, which the new rule
removes. The first now adds without recalculating; the second uses a
real automatic reward.

## Testing
- 14 new tests in `ReconcileRewardLinesTest` and one in
`DeleteDiscountTest`. Each fails on `2.x` and passes here, on SQLite,
MySQL and PostgreSQL (`cross-db` group).
- Suites: core, admin, panel, filament, shipping, stripe, paypal,
bundles, search, upgrade all pass. PHPStan and Pint clean. `mint
validate` and `mint broken-links` pass.

Not changed here, found while testing: running `CreateOrderLines`
directly on a cart with two different variants under
`preventLazyLoading` fails on `2.x` too, because it lazy-loads `values`.
Carts loaded through `CartSession` (with `lunar.cart.eager_load`) aren't
affected.

---------

Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant