Repository navigation
docs(specs): remove reward lines the cart no longer earns - #2937
Merged
Merged
Conversation
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
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New spec,
remove-unearned-reward-lines. It follows up oncore-values-out-of-meta(#2861): a line thatBuyXGetYadds automatically stays in the cart, and is charged at full price, once the cart stops earning it.What happens on
2.xtoday (each case was run against8ab3f9da; "buy A get B free", both £10 with 20% tax)$cart->freeItemsgains an entry on every recalculation of the same instance.added_by_discount_id, so its lines become ordinary shopper lines at full price.The proposal
BuyXGetYonly allocates: it keeps existing added lines first, then raises them, then adds lines. It no longer saves or prices lines.ReconcilesRewardLinesaction creates, resizes or deletes added lines from the discount breakdown.CalculateCartruns the calculation steps once more when the action changed anything, so shipping and tax see the corrected lines.CartLineAddedByDiscountvalidator refuses quantity changes to an added line (one new string in 16 locales).Behaviour changes to review
PercentageOffandFixedAmountOffno longer see added lines, so a free gift no longer counts toward any minimum spend.automatically_add_rewardsremoves 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.