Shop discount script
Website rewriteResets at 5:30 PM
Shop discount script
Website rewriteSearch chats or run a command
Write a short script for the online shop: 10% off a subscription’s first box with the code FIRSTBOX.
Wrote the document
{ "documentId": "first-box-discount", "kind": "code", "title": "First box discount"}{ "documentId": "first-box-discount", "kind": "code", "title": "First box discount", "version": 1}The script checks the code at checkout, takes 10% off the first box only, and leaves later boxes at full price. It refuses the code on a one-off order.
Refuse the code if the customer has used it before.
Changed the document
{ "documentId": "first-box-discount", "kind": "code", "title": "First box discount"}{ "documentId": "first-box-discount", "kind": "code", "title": "First box discount", "version": 2}It now checks the codes the customer has used, and refuses FIRSTBOX on a second subscription.
Orion can make mistakes. Check what matters.
By Orion ·
interface Order { type: 'subscription' | 'one-off' /** 1 for the first box of a subscription. */ boxNumber: number /** In pence. */ subtotal: number discount: number total: number /** The codes this customer has used before. */ usedCodes: string[]}
const CODE = 'FIRSTBOX'const DISCOUNT = 0.1
export function applyFirstBoxDiscount(order: Order, code: string): Order { if (code.trim().toUpperCase() !== CODE) return order if (order.type !== 'subscription' || order.boxNumber !== 1) { throw new Error('FIRSTBOX works on a subscription’s first box only.') } if (order.usedCodes.includes(CODE)) { throw new Error('FIRSTBOX has been used on this account already.') } const discount = Math.round(order.subtotal * DISCOUNT) return { ...order, discount, total: order.subtotal - discount }}