Skip to content

Discount coupons: many codes per discount - #2857

Merged
glennjacobs merged 8 commits into
2.xfrom
feat/discount-coupons
Nov 26, 2026
Merged

glennjacobs merged 8 commits into
2.xfrom
feat/discount-coupons

Conversation

@glennjacobs

Copy link
Copy Markdown
Contributor

Implements slices 2 and 3 of spec discount-coupons (#2848), plus code management in both admins: a discount can have many coupon codes. Third of four; based on the guest limits PR.

Summary

  • Core. New coupons table and Coupon model; discounts.coupon dropped; discounts.redemption added (Enums\DiscountRedemptionMethod: Automatic, Coupon). The spec names the enum DiscountRedemption, which clashes with the redemption model. Cart::coupon() resolves the code, and DiscountManager, AbstractDiscountType and CouponValidator all match through it. The order's discount breakdown carries coupon_id and coupon_code. Placement increments coupons.uses atomically and reports a coupon over-limit. CreatesCoupon and DeletesCoupon actions.
  • Upgrade. Each v1 coupon becomes a one-row coupon, and every discount gets its redemption. Safe to re-run.
  • Panel. Redemption choice replaces the coupon field. A Coupons section lists codes (search, uses, limit, email, batch, last redeemed) and adds and deletes them. The index filter and coupon column read the new model. Order timeline labels for the three limit kinds.
  • Filament/admin. Not covered by the spec, but it read discounts.coupon: a redemption select and a CouponRelationManager.
  • Translations in all 16 locales; docs and upgrade guide updated.

Notes for review

  • Migration numbers: coupons is 000073 and discount_redemptions moves to 000074, so its coupon_id foreign key has a table to point at.
  • Panel codes save immediately rather than with the discount draft.
  • Non-English strings were machine-assisted. The word for "batch" is least certain in fa, tr, mn and hu.

Checks

core, upgrade, admin, shipping and stripe suites pass; phpstan, pint, panel vitest/type-check/build and mint pass. Failures that also fail on 2.x: panel StaffSessionInvalidationTest, filament ThumbnailImage (4), search Typesense "resolves the collections index".

🤖 Generated with Claude Code

@github-project-automation github-project-automation Bot moved this to Todo in Roadmap Nov 12, 2026
@github-actions github-actions Bot added needs-triage Base branch is not a recognised release line high and removed 2.x labels Nov 12, 2026
@glennjacobs
glennjacobs added this pull request to stack #2927 November 26, 2026 19:59
@glennjacobs
glennjacobs force-pushed the feat/discount-coupons branch 2 times, most recently from f56bd6a to 9fbd65f Compare November 26, 2026 20:23
Base automatically changed from feat/guest-discount-limits to 2.x November 26, 2026 20:23
glennjacobs and others added 8 commits November 26, 2026 20:23
Coupons become rows on a new coupons table. A discount's redemption column
says whether it applies automatically or needs a coupon, and a cart's code
resolves through Cart::coupon(). Placement records the coupon on the
redemption, counts its use atomically and reports a code over its own limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new data step turns each v1 discounts.coupon into a coupons row carrying
the discount's uses and max_uses, sets redemption, and drops the column. It
runs before the discount_user copy, whose mirrored discount_redemptions now
references coupons. The ledger baseline lists the coupons migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CreatesCoupon adds one code to a discount and DeletesCoupon removes one,
keeping its redemptions without the coupon. The panel and Filament call
them rather than writing the rows themselves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The discount edit page replaces the coupon field with "Applied
automatically" / "Needs a coupon", and a coupon discount gets a Coupons
section: a searchable, paginated table of codes, adding one code with an
optional limit, and deleting a code. The index filters on redemption and
shows the code or the count. The order timeline labels
discount-limit-exceeded by the limit that was passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The discount form picks a redemption method instead of a coupon code, the
table shows the code or the count, and coupon discounts get a relation
manager to list, search, add and delete codes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Documents the Coupon model, Discount::$redemption, Cart::coupon(), the
coupon on redemptions and order breakdowns, the coupon limit in
DiscountLimitExceeded, and the upgrade step and manual notes for
Discount::$coupon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Implements slice 4 of spec `discount-coupons` (#2848), the generation
and export parts of slice 5, and slice 6. Last of four; based on the
coupons PR.

- `CouponBatch` describes a run: `ofQuantity()` or `forEmails()`
(distinct, normalised addresses), with length, prefix and uses per code.
- `CouponCodeGenerator` contract; the default leaves out `0 O 1 I L`.
- `GenerateCoupons` inserts in chunks of 1,000 with `insertOrIgnore` and
tops up until the batch is complete, so collisions are replaced. The
batch ULID is fixed when the `CouponBatch` is made, so a retried job
finishes the same batch instead of duplicating it.
`Discount::generateCoupons()` is the verb; `GenerateCouponsJob` queues
it.
- Panel: Generate codes (quantity, or emails pasted or uploaded as CSV)
and Export to CSV for all codes or one batch. Runs above
`lunar.discounts.coupons.queue_threshold` are queued and shown as
generating until done.
- Docs: generation reference and a new guide for a single-use code
campaign with guest checkout.

Set to 5,000 from a timing run into a table already holding 200k codes,
`sync` queue:

| Database | 1k | 5k | 10k | 50k |
|:--|:--|:--|:--|:--|
| SQLite | 19ms | 63ms | 137ms | 0.90s |
| MySQL 8 | 28ms | 105ms | 215ms | 1.10s |
| Postgres 18 | 36ms | 142ms | 280ms | 1.46s |

The insert dominates; code generation is about 5% of the time. Email
batches cost about 1.4x. Rerun with `vendor/bin/pest
tests/core/Benchmarks` (`DB_DRIVER=mysql|pgsql`).

- A queued email batch carries its address list in the job payload,
about 1.25MB for 50k addresses. Fine on Redis and database queues, too
large for SQS.
- The generating state is a cache entry per discount with no lock, so
two staff queueing on the same discount at the same instant can lose one
progress marker. The codes still generate.

All suites pass apart from failures that also fail on `2.x` (panel
`StaffSessionInvalidationTest`, filament `ThumbnailImage` (4), search
Typesense). phpstan, pint, panel vitest/type-check/build and mint pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Amp <amp@ampcode.com>
MapDiscountBreakdownTest creates its coupon through the coupons relation,
the discount edit page test passes the redemption and coupon props, and the
redemption draft tests send their data with the commit, which 2.x's draft
layer now expects.

Amp-Thread-ID: https://ampcode.com/threads/T-01a217b3-1893-73f9-b128-3754ed5c50e2
Co-authored-by: Amp <amp@ampcode.com>
@glennjacobs
glennjacobs force-pushed the feat/discount-coupons branch from 9fbd65f to cbfbedd Compare November 26, 2026 20:23
@glennjacobs
glennjacobs merged commit 4cf42bf into 2.x Nov 26, 2026
31 of 32 checks passed
@glennjacobs
glennjacobs deleted the feat/discount-coupons branch November 26, 2026 20:23
@github-project-automation github-project-automation Bot moved this from Todo to Done in Roadmap Nov 26, 2026
@glennjacobs glennjacobs removed the needs-triage Base branch is not a recognised release line label Nov 26, 2026
@glennjacobs glennjacobs added the 2.x label Nov 26, 2026 — with Amp for GitHub
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>
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