Models
Shopper uses two models to manage discounts:Database Schema
Discount Table
| Column | Type | Nullable | Default | Description |
|---|---|---|---|---|
id | bigint | no | auto | Primary key |
code | string | no | - | Unique discount code |
type | string | no | - | Discount type enum |
value | integer | no | - | Discount value (percentage or cents) |
is_active | boolean | no | false | Discount visibility |
apply_to | string | no | - | Application scope enum |
min_required | string | no | - | Requirement type enum |
min_required_value | string | yes | null | Minimum value required |
eligibility | string | no | - | Eligibility type enum |
usage_limit | integer | yes | null | Maximum total uses |
usage_limit_per_user | boolean | no | false | Limit one use per customer |
total_use | integer | no | 0 | Current total usage count |
start_at | datetime | no | - | Discount start date |
end_at | datetime | yes | null | Discount end date (null = no expiry) |
zone_id | bigint | yes | null | FK to zone (null = all zones) |
metadata | json | yes | null | Additional custom data |
created_at | timestamp | yes | null | Creation timestamp |
updated_at | timestamp | yes | null | Last update timestamp |
Discountable Table (Pivot)
| Column | Type | Nullable | Description |
|---|---|---|---|
id | bigint | no | Primary key |
condition | string | yes | Condition type (apply_to, eligibility) |
total_use | integer | no | Usage count for this relation |
discountable_id | bigint | no | Polymorphic relation ID |
discountable_type | string | no | Polymorphic relation type |
discount_id | bigint | no | FK to discount |
Discount Type
TheDiscountType enum defines value types:
| Type | Database Value | Value Handling |
|---|---|---|
| Percentage | percentage | Stored as-is (10 = 10%) |
| Fixed Amount | fixed_amount | Stored in cents (1000 = $10.00) |
Discount Application
TheDiscountApplyTo enum defines where the discount applies:
| Scope | Database Value | Description |
|---|---|---|
| Order | order | Discount applies to total order amount |
| Products | products | Discount applies only to linked products |
Discount Eligibility
TheDiscountEligibility enum defines who can use the discount:
| Eligibility | Database Value | Description |
|---|---|---|
| Everyone | everyone | Any customer can use |
| Customers | customers | Only linked customers can use |
Discount Requirements
TheDiscountRequirement enum defines minimum conditions:
| Requirement | Database Value | min_required_value |
|---|---|---|
| None | none | Not used |
| Price | price | Minimum amount in cents |
| Quantity | quantity | Minimum item count |
Relationships
Items (DiscountDetail)
Zone
HasDiscounts Trait
Models can use theHasDiscounts trait to access their discounts:
Value Handling
Fixed amount values are automatically converted:Usage Limit Checking
Creating Discounts
Percentage Discount for All Orders
Fixed Amount Discount with Minimum Order
Product-Specific Discount
VIP Customer Discount
Retrieving Discounts
Validating Discounts
Components
config/shopper/components/discount.php:
Storefront Example
Use Cases
| Scenario | Type | Apply To | Eligibility | Requirement |
|---|---|---|---|---|
| Site-wide sale | Percentage | Order | Everyone | None |
| Free shipping over $50 | Fixed | Order | Everyone | Price |
| Buy 3+ get 10% off | Percentage | Order | Everyone | Quantity |
| VIP member exclusive | Percentage | Order | Customers | None |
| Product clearance | Percentage | Products | Everyone | None |
| First order discount | Fixed | Order | Customers | None |