Skip to content

Remove reward lines the cart no longer earns (spec remove-unearned-reward-lines) - #2938

Merged
glennjacobs merged 5 commits into
2.xfrom
feat/remove-unearned-reward-lines
Nov 27, 2026
Merged

glennjacobs merged 5 commits into
2.xfrom
feat/remove-unearned-reward-lines

Conversation

@glennjacobs

Copy link
Copy Markdown
Contributor

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.

glennjacobs and others added 4 commits November 27, 2026 14:13
…arned-reward-lines, 1/4)

A line a discount added now holds exactly the reward units that discount
allocates to it in the current calculation. BuyXGetY allocates its
shortfall onto its own lines (keep, then raise, then add) without writing
them; the new ReconcilesRewardLines action creates, resizes or deletes
the lines from the breakdown, and CalculateCart runs the calculation
steps once more when it changed any, so shipping and tax see them.

Lines a discount added no longer count toward any discount's conditions,
minimum spend or targeted lines, so a reward can't keep itself earned.
ApplyDiscounts resets freeItems on each run.

Marks the spec accepted and slice 1 done.

Amp-Thread-ID: https://ampcode.com/threads/T-8ecb4cbd-8657-4f41-8f71-6de8c214834c
Co-authored-by: Amp <amp@ampcode.com>
…arned-reward-lines, 2/4)

Deleting a discount deletes the lines it added on carts without a
completed order. The foreign key is null on delete, so they would
otherwise stay as ordinary lines at full price. DiscountObserver runs it
on every delete path, one model at a time so each cart's persisted totals
go stale. Carts with a completed order keep their lines as history.

Amp-Thread-ID: https://ampcode.com/threads/T-8ecb4cbd-8657-4f41-8f71-6de8c214834c
Co-authored-by: Amp <amp@ampcode.com>
…arned-reward-lines, 3/4)

CartLineAddedByDiscount joins the UpdateCartLineValidation steps. It
fails when the requested quantity differs on a line a discount added,
since the calculation sizes that line to the reward units its discount
allocates and would undo the change. A meta-only update at the same
quantity passes. The message is translated into all 16 locales.

Amp-Thread-ID: https://ampcode.com/threads/T-8ecb4cbd-8657-4f41-8f71-6de8c214834c
Co-authored-by: Amp <amp@ampcode.com>
…eward-lines, 4/4)

Describes how added reward lines are kept, raised, lowered and removed,
the refused quantity change, removal when a discount is deleted, how a
custom discount type has core manage a line it adds, and the second pass
of the calculation steps. Adds the behaviour note to the alpha upgrade
guide.

Amp-Thread-ID: https://ampcode.com/threads/T-8ecb4cbd-8657-4f41-8f71-6de8c214834c
Co-authored-by: Amp <amp@ampcode.com>
The cross-db test for a deleted discount's line still expected every line
to stay as an ordinary line. Slice 2 deletes the line on a cart still
being shopped, so the test now places the cart first and checks that its
line keeps the history with the key nulled. It is skipped on SQLite, which
does not enforce the foreign key, so the local run missed it.

Amp-Thread-ID: https://ampcode.com/threads/T-8ecb4cbd-8657-4f41-8f71-6de8c214834c
Co-authored-by: Amp <amp@ampcode.com>
glennjacobs added a commit that referenced this pull request Nov 27, 2026
…ed (#2939)

Tracker housekeeping. No code changes.

- Flips four specs to `implemented`, ticks their slices, and moves them
to `specs/completed/`. Each was checked against `2.x`:
  - `attribute-descriptions`: #2881.
- `core-values-out-of-meta`: the `product_option_values.colour` column
and `added_by_discount_id` on cart and order lines are in the baselines.
- `discount-coupons`: #2855 (usage counted at placement) and #2857
(coupons, upgrade, generation, panel, docs including the single-use
coupon campaign guide).
- `guest-discount-usage-limits`: #2856 (`ResolvesCustomerEmail`,
`CountsCustomerRedemptions`, `CouponValidator` taking the cart,
`ValidateCustomerForDiscounts`, docs).
- Moves their `TODO.md` entries from Outstanding to Done, along with
0083, whose spec was already marked implemented and moved to
`completed/` in #2926.
- No paths outside `specs/` referenced the moved files.

`remove-unearned-reward-lines` stays in Outstanding until #2938 merges.

Co-authored-by: Amp <amp@ampcode.com>
@glennjacobs
glennjacobs merged commit 14c6781 into 2.x Nov 27, 2026
28 checks passed
@glennjacobs
glennjacobs deleted the feat/remove-unearned-reward-lines branch November 27, 2026 14:41
@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
…t to completed (#2940)

Tracker housekeeping. No code changes.

- Sets `remove-unearned-reward-lines` to `implemented` (all four slices
shipped in #2938) and moves it to `specs/completed/`.
- Moves its `TODO.md` entry from Outstanding to Done.
- No paths outside `specs/` referenced the file.

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