Promotions Engine
v1.xCart promotions package for Basketin — discount rules, coupons and a promotion engine for Laravel e-commerce.
Cart Promotions
Cart promotions package for Basketin — discount rules, coupons and a promotion engine for Laravel e-commerce.
Installation
composer require basketin/cart-promotions
The service provider is auto-discovered by Laravel.
Usage
Creating a Promotion Rule
Create a class that extends Promotion and implements PromotionRule:
use Obelaw\Basketin\Cart\Promotions\Contracts\PromotionRule;
use Obelaw\Basketin\Cart\Promotions\Promotion;
use Obelaw\Basketin\Cart\Services\CartService;
class MyPromoRule extends Promotion implements PromotionRule
{
protected ?string $name = 'My Promotion';
public function calculate(CartService $cart): float
{
// Return the discount amount
return 100;
}
}
Applying Promotions
Use the promotions() macro on the totals service:
$cart = CartManagement::make('cart_id', 'USD');
$cart->quote()->addQuote($product, 1);
$totals = $cart->totals();
$totals->promotions()
->rule(new MyPromoRule())
->apply();
$discountedTotal = $totals->getGrandTotal();
Multiple Rules
You can chain multiple rules:
$totals->promotions()
->rule(new RuleOne())
->rule(new RuleTwo())
->apply();
Getting Applied Rules
After applying promotions, you can retrieve the applied rules:
$totals->promotions()
->rule(new MyPromoRule())
->apply();
$appliedRules = $totals->promotions()->getAppliedRules();
// Returns array of applied rules with name, discount_amount, and rule_type
Architecture
PromotionEngine
Main class that processes promotion rules against a cart.
Methods:
rule(PromotionRule $rule)- Add a promotion ruleapply()- Apply all rules and calculate discountsgetAppliedRules()- Get list of applied rules with details
Promotion (Abstract Class)
Base class for promotions. Provides getName() method.
PromotionRule (Interface)
Contract for promotion rules. Requires:
getName(): string- Return the promotion namecalculate(CartService $cart): float- Calculate discount amount