Overview
A single-use code campaign gives each recipient their own code for one discount, and each code can be redeemed once. This guide covers creating the discount, generating a code per email address, exporting the codes for a mail merge, and what happens when a guest redeems a code at checkout. The code is the credential. A guest does not need to sign in, and the cart does not need a customer: whoever holds a code can redeem it once. This differs frommax_uses_per_user, which limits each customer but lets anyone who sees a shared code use it. See Per-customer limits.
Creating the Discount
Create a discount whoseredemption is coupon, so it only applies to a cart with one of its codes:
max_uses. For example, 5,000 single-use codes with max_uses of 1,000 stop applying after 1,000 orders.
Generating a Code per Recipient
CouponBatch::forEmails() describes one code per distinct email address. Each code has max_uses of 1 by default, and records the address in its email column:
The example below sends a code to everyone who has placed an order, guests included, using the billing contact email of past orders:
@ is read as an address, so a CSV export from a mailing tool works without editing. Batches larger than lunar.discounts.coupons.queue_threshold codes (default 5000) are generated on the queue, and the panel shows the batch as generating until it is complete. A queue worker must be running for the batch to finish.
See Generating codes for every option.
Exporting Codes for a Mail Merge
In the admin panel, Export to CSV downloads every code of the discount. To export one batch, select the batch ULID in the table’s Batch column. The file has the columnscode, email, uses and redeemed_at, ready for an email platform’s mail merge.
To send the emails from the store instead, read the batch from the discount:
App\Mail\SpringThankYou stands for the store’s own mailable.
Redeeming a Code at Guest Checkout
A guest applies the code like any other coupon, by setting the cart’scoupon_code. See Applying a Coupon. To give a clear message when the code is unknown or already used, check it first:
email. A recipient who checks out with a different address can still redeem their code, and a forwarded code can still be redeemed only once.
When a Code Counts as Used
A code is used when its order is placed, not when the draft order is created. A declined card or an abandoned checkout does not use the code, so a guest who retries, on the same cart or on a new one, still gets the discount. When the order is placed, Lunar records aLunar\Core\Models\DiscountRedemption for the code and increments the code’s uses. See Usage.
Two carts can be paid for with the same code at the same moment, before either order is placed. Both orders stand, because both have been paid for. Lunar dispatches Lunar\Core\Events\Discounts\DiscountLimitExceeded and logs an activity on the second order, so the merchant can see it. See Going over a limit.
Tracking the Campaign
Each code’suses shows whether it has been redeemed, and its redemptions relationship links to the orders:
Next Steps
- Review the Discounts reference for coupons, redemptions, and limits.
- Review the Cart guide for applying and removing coupon codes.
- Review the Checkout guide for guest checkout.