Discount codes and automatic discounts: percent, fixed, free shipping, product/category scope, usage and first-order limits
Install
genpm add @core/discountsWhat you get
- Source in src/lib/discounts/, 6 files. (21.5 kB)
- AI rules in src/lib/discounts/AGENTS.md, plus IDE rule files.
- Resolves @core/cart, @core/catalog, @core/db, @core/money, @core/orders for you.
README
This package has no README.
This is exactly what your AI reads when it works in src/lib/discounts. Nothing else is added to its context.
@core/discounts — rules for AI agents
Purpose
Discount codes and automatic discounts for @core/cart: percent, fixed amount or free shipping; optional scope by
product or category slugs, minimum order, start/end dates, total and per-customer usage limits, first-order-only and
combinability (non-combinable ones compete: the biggest wins). Registers the discounts (100) and free-shipping
(250) totals steps and records redemptions inside the transaction that creates/pays the order (onOrderPaidTx of
@core/orders), with the discount row locked. Limits count recorded redemptions plus open payments carrying the code
(pendingCodeUses of @core/cart, fed by @core/checkout). If a limit was exceeded anyway (the customer already paid),
the order is kept, the redemption recorded and a discount_over_limit event is added to the order timeline for review.
Tables: discounts, discount_redemptions.
Map
index.ts— public API:upsertDiscount,DiscountInput,discountsStep,freeShippingStep,recordRedemptions,discountsAdminResource.discounts.ts— validation, application and the redemption handler (registered on import).admin.ts— admin resource.
Integration
- Migrations as in
src/lib/db/AGENTS.md. Import this module at startup (it registers its steps and handler). - Cart page: a code field calling
PUT /api/cart/codes {codes: [code]}(@core/cart); showtotals.discountsand anyinvalid_codeissues. - Pass the customer email to
computeTotals(cart, { email })once known so per-customer and first-order limits apply. - Add
discountsAdminResourcetosrc/genpm/admin.ts(permissionsdiscounts:read|update). - Verify: create
TEN(10 %), apply it, the total drops and each line shows its share.
Conventions
- Codes are stored uppercase (
A–Z 0–9 _ -); values: percent 1–100, fixed in minor units. - Deleting a discount deactivates it (redemption history stays).
- Free-shipping adjustments carry their
code(amount 0), so they count towards usage limits. - Check the order timeline (
discount_over_limit) when a usage-limited campaign ends. - Prefer automatic discounts for store-wide sales; codes for campaigns.
Don't
- Don't show a "was" price or a percentage off that isn't real: in the EU a price reduction must refer to the lowest price of the previous 30 days (Omnibus Directive). Use @core/pricing's price history for compare-at prices.
- Don't apply discounts on the client or trust discount amounts sent by the browser.
- Don't create fake urgency (countdowns that reset, "only today" when it isn't).
# @core/discounts — rules for AI agents
## Purpose
Discount codes and automatic discounts for @core/cart: percent, fixed amount or free shipping; optional scope by
product or category slugs, minimum order, start/end dates, total and per-customer usage limits, first-order-only and
combinability (non-combinable ones compete: the biggest wins). Registers the `discounts` (100) and `free-shipping`
(250) totals steps and records redemptions inside the transaction that creates/pays the order (`onOrderPaidTx` of
@core/orders), with the discount row locked. Limits count recorded redemptions plus open payments carrying the code
(`pendingCodeUses` of @core/cart, fed by @core/checkout). If a limit was exceeded anyway (the customer already paid),
the order is kept, the redemption recorded and a `discount_over_limit` event is added to the order timeline for review.
Tables: `discounts`, `discount_redemptions`.
## Map
- `index.ts` — public API: `upsertDiscount`, `DiscountInput`, `discountsStep`, `freeShippingStep`, `recordRedemptions`, `discountsAdminResource`.
- `discounts.ts` — validation, application and the redemption handler (registered on import). `admin.ts` — admin resource.
## Integration
1. Migrations as in `src/lib/db/AGENTS.md`. Import this module at startup (it registers its steps and handler).
2. Cart page: a code field calling `PUT /api/cart/codes {codes: [code]}` (@core/cart); show `totals.discounts` and any `invalid_code` issues.
3. Pass the customer email to `computeTotals(cart, { email })` once known so per-customer and first-order limits apply.
4. Add `discountsAdminResource` to `src/genpm/admin.ts` (permissions `discounts:read|update`).
5. Verify: create `TEN` (10 %), apply it, the total drops and each line shows its share.
## Conventions
- Codes are stored uppercase (`A–Z 0–9 _ -`); values: percent 1–100, fixed in minor units.
- Deleting a discount deactivates it (redemption history stays).
- Free-shipping adjustments carry their `code` (amount 0), so they count towards usage limits.
- Check the order timeline (`discount_over_limit`) when a usage-limited campaign ends.
- Prefer automatic discounts for store-wide sales; codes for campaigns.
## Don't
- Don't show a "was" price or a percentage off that isn't real: in the EU a price reduction must refer to the lowest
price of the previous 30 days (Omnibus Directive). Use @core/pricing's price history for compare-at prices.
- Don't apply discounts on the client or trust discount amounts sent by the browser.
- Don't create fake urgency (countdowns that reset, "only today" when it isn't).
The exact tree that will be injected, after .genpmignore. Pinned to
MIT License
Copyright (c) 2026 GenPM
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This package declares no MCP servers.
| Version | Commit | Published | Scan |
|---|---|---|---|
| 1.0.1 | 6b23d34 | 3 hours ago | scan passed |
- npm
- zod ^4.0.0
- proposed
- GenPM proposes the npm command and runs it only if you say yes.
- Used by (1)
- @core/kit-store ^1.0.0
- scan
- scan passed · 0 findings
- commit
- v1.0.1 → 6b23d346ddd5be7a9154a7106859687008386baa · verified after fetch
- scripts
- None. GenPM never runs package code.
- license
- MIT
- Quality
- 100/100
- Recognized licensepassed
- AGENTS.md explains its purposepassed
- AGENTS.md has integration stepspassed
- AGENTS.md lists conventions or don'tspassed
- Includes testspassed
- Security scan passedpassed
- Released in the last 6 monthspassed
- Verified publisherpassed
- Summary and keywordspassed
- report
- See something wrong?