Discount types & composing
The string-backed DiscountType enum names how a coupon’s value applies; the math itself belongs to money-for-laravel’s Discount:
| Case | Value | Stored value means |
|---|---|---|
Fixed | fixed | Minor units of the coupon’s currency. |
Percentage | percentage | Basis points (2500 = 25 %). |
FreeShipping | free_shipping | Ignored — a marker for the host’s shipping line. |
use RoundlyConsulting\Coupons\Enums\DiscountType;
use RoundlyConsulting\Money\Currency;
use RoundlyConsulting\Money\Money;
$eur = Currency::of('EUR');
DiscountType::Fixed->toDiscount(250, $eur)->applyTo(Money::ofMinor(1000, 'EUR')); // 7.50 EUR
DiscountType::Percentage->toDiscount(1250, $eur)->amountFor(Money::ofMinor(999, 'EUR')); // 1.25 EUR (12.5 %, half away from zero)
DiscountType::Percentage->toDiscount(5000, $eur, cap: Money::ofMinor(300, 'EUR')); // 50 % capped at 3.00 EUR
// Free shipping is a discount on the SHIPPING target — route by $discount->target(); never
// apply it to the price of the goods.
DiscountType::FreeShipping->toDiscount(0, $eur)->target(); // DiscountTarget::Shipping
// Translatable labels and descriptions for admin UIs:
DiscountType::Percentage->label(); // "Percentage"
DiscountType::Percentage->description(); // "Subtracts a percentage of the price."
DiscountType::FreeShipping->requiresValue(); // false (Fixed/Percentage are true)Admin labels
label() and description() are translated through coupons::messages.type.*, so building a select for an admin form is one line:
collect(DiscountType::cases())->mapWithKeys(fn ($t) => [$t->value => $t->label()]);
// Collection: ['fixed' => 'Fixed amount', 'percentage' => 'Percentage', 'free_shipping' => 'Free shipping']Composing with other discounts
Coupon::discount(Currency $for) hands you the coupon as a money Discount — labelled with the code, capped at max_discount, and in the coupon’s own currency when it’s locked. Combine it with other discounts in a DiscountStack:
use RoundlyConsulting\Money\Currency;
use RoundlyConsulting\Money\Discounts\Discount;
use RoundlyConsulting\Money\Discounts\DiscountStack;
use RoundlyConsulting\Money\Money;
$subtotal = Money::ofMinor(8000, 'EUR');
$shipping = Money::ofMinor(500, 'EUR');
$loyalty = Discount::percentage(5)->labelled('Loyalty');
$breakdown = DiscountStack::of(
$coupon->discount(Currency::of('EUR')),
$loyalty,
)->apply($subtotal, $shipping);
$breakdown->discountedSubtotal();
$breakdown->discountedShipping(); // a free-shipping coupon targets shipping here
$breakdown->total();A free-shipping coupon yields a shipping-target discount, so it lands on the shipping line of the breakdown, never on the goods. DiscountType::toDiscount(int $value, Currency $currency, ?Money $cap = null) builds the same Discount from raw values.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.