Skip to content

Add Discount Functionality #1

@heywinit

Description

@heywinit

I came across the project on X today. I could really use this for an upcoming project I have. The only thing I'm currently missing is discounts, and I would like to add those here. I've written down a basic plan on what I'm looking forward to adding. Let me know if you'd like any changes.

Aim: Add support for discount codes for carts (percentage, fixed amount, free shipping).

API

  • Admin: GET|POST|PATCH|DELETE /v1/discounts
  • Public: POST /v1/carts/:id/apply-discount, DELETE /v1/carts/:id/discount

Discount flow

  1. Discount validation (active, dates, limits, min purchase)
  2. Calculating discount on subtotal of the cart
  3. Update cart totals to include discount
  4. Integrate with Stripe Checkout (allow_promotion_codes)
  5. Store discount in orders via webhook
  6. Tracking discount code usage

Types of discount

  • Relative: 20% off, max $50
  • Absolute: $10 off orders over $50
  • Free shipping: free shipping on orders over $25

DB changes

  • Adding two new tables, discounts and discounts_usage
discounts table
- id – text, primary key
- store_id – text, not null, foreign key → stores(id)
- code – text, unique
- type – text, not null, enum (percentage, fixed_amount, free_shipping)
- value – integer, not null
- status – text, not null, default active, enum (active, inactive)
- min_purchase_cents – integer, default 0
- max_discount_cents – integer, nullable
- starts_at – text, nullable
- expires_at – text, nullable
- usage_limit – integer, nullable
- usage_count – integer, not null, default 0
- stripe_coupon_id – text, nullable
- stripe_promotion_code_id – text, nullable
- created_at – text, not null, default now
- updated_at – text, not null, default now

discount_usage table
- id – text, primary key
- discount_id – text, not null, foreign key → discounts(id)
- order_id – text, not null, foreign key → orders(id)
- customer_email – text, not null
- discount_amount_cents – integer, not null
- created_at – text, not null, default now
  • Changes to existing tables
-- carts
ALTER TABLE carts ADD COLUMN discount_code TEXT;
ALTER TABLE carts ADD COLUMN discount_id TEXT REFERENCES discounts(id);
ALTER TABLE carts ADD COLUMN discount_amount_cents INTEGER DEFAULT 0;

-- orders
ALTER TABLE orders ADD COLUMN discount_code TEXT;
ALTER TABLE orders ADD COLUMN discount_id TEXT REFERENCES discounts(id);
ALTER TABLE orders ADD COLUMN discount_amount_cents INTEGER DEFAULT 0;

Misc

  • Single discount per cart. Validate on cart update and checkout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions