Discount Logic
The Pizza Chef Frontend implements an automatic bulk discount system to encourage larger orders. When a customer orders 3 or more of the same pizza, they receive a 10% discount on that line item.Discount Rule
Discount Policy: 10% off when ordering 3 or more of the same pizzaThe discount applies per pizza type, not to the total order. Each unique pizza in the cart is evaluated independently.
Implementation
The discount calculation is implemented inorderSlice.ts through the calculateItemTotals function:
Calculation Breakdown
- Original Line Price:
pizza.price * quantity - Discount Amount:
originalLinePrice * 0.1(if quantity >= 3) - Final Line Total:
originalLinePrice - discountAmount
OrderItem Structure
Each item in the cart includes discount information:When Discounts Are Applied
ThecalculateItemTotals function is called in three scenarios:
1. Adding to Cart
2. Updating Quantity
3. Direct Item Addition
When a new pizza is added to the cart, it’s immediately run throughcalculateItemTotals.
Real-Time Updates
Discounts are calculated immediately whenever the cart changes:- No checkout required to see savings
- Discount appears as soon as the 3rd item is added
- Removing items below the threshold removes the discount instantly
Example Scenarios
Scenario 1: Single Pizza Type
Order: 4x Margherita Pizza @ $12.00 eachScenario 2: Multiple Pizza Types
Order:- 5x Pepperoni @ $15.00 each
- 2x Vegetarian @ $13.00 each
Scenario 3: Exactly 3 Items (Edge Case)
Order: 3x Hawaiian @ $14.00 each>= operator ensures that exactly 3 items qualifies for the discount.
Order Totals
When an order is finalized, the complete order includes:totalDiscount aggregates savings across all line items that qualified.
Persistence
Current cart state (including calculated discounts) is saved to localStorage:pizza_current_order
When the app reloads, the full OrderItem structure (including discountAmount and finalLineTotal) is restored, so customers don’t lose their calculated savings.
UI Display Considerations
While the discount logic is in Redux, the UI typically shows:- Original price per item
- Quantity
- Line total (discounted if applicable)
- Discount badge or indicator when discount is active
- Total savings in the order summary
The actual discount amount is stored in the state, but displaying it prominently in the UI helps customers understand the value they’re receiving and may encourage them to add more items to reach the threshold.
Why 10% at 3+ Items?
This threshold was chosen because:- Psychological Impact: Simple, easy-to-understand rule
- Business Value: Encourages bulk orders without excessive margin loss
- UX Clarity: No complex tiers or calculations to confuse customers
- Incremental Incentive: Customers with 2 items are motivated to add a 3rd
Extensibility
To modify the discount logic, update thecalculateItemTotals function:
Testing the Discount Logic
Key test cases:- Quantity = 2: No discount applied
- Quantity = 3: Exactly 10% discount
- Quantity = 10: 10% discount (not tiered)
- Multiple items: Each calculated independently
- Increment from 2 → 3: Discount appears
- Decrement from 3 → 2: Discount disappears
- Persistence: Reload page, discount values intact
Related Files
src/store/orderSlice.ts:27-44- Discount calculation functionsrc/store/orderSlice.ts:53-78- Actions that trigger recalculationsrc/types/order.ts:3-9- OrderItem interface