noxoua / filament-coupons
A flexible coupon management system for Filament
Fund package maintenance!
noxoua
Requires
- php: ^8.1
- filament/filament: ^3.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.1
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
Filament Coupons
A flexible coupon management system for Filament 3.x with customizable strategies and usage tracking.
Installation
composer require noxoua/filament-coupons php artisan filament-coupons:install
Setup
Add the plugin to your Filament panel:
use Noxo\FilamentCoupons\CouponsPlugin; public function panel(Panel $panel): Panel { return $panel ->plugins([ CouponsPlugin::make(), ]); }
Usage
Creating Strategies
Create custom coupon strategies:
php artisan make:coupons-strategy FreeSubscription
Register in config:
// config/filament-coupons.php 'strategies' => [ \App\Coupons\FreeSubscriptionStrategy::class, ],
Strategy Example
class FreeSubscriptionStrategy extends CouponStrategy { public function schema(): array { return [ Forms\Components\TextInput::make('days') ->label('Days') ->numeric() ->required(), ]; } public function apply(Coupon $coupon): bool { $user = auth()->user(); $days = $coupon->payload['days'] ?? 7; // Your business logic $user->extendSubscription($days); // Configure notifications and redirects $this->successNotification( fn ($notification) => $notification ->title('Coupon Applied!') ->body("You got {$days} free days") ); $this->successRedirectUrl('/dashboard'); // Consume coupon return coupons()->consume($coupon, couponable: $user); } }
Available methods for strategies:
- Custom notifications
successNotification
failureNotification
- Custom redirects
successRedirectUrl
failureRedirectUrl
Using the Action
The package provides a ready-to-use ApplyCouponAction
that can be integrated anywhere in your Filament application:
In Livewire Components:
use Noxo\FilamentCoupons\Actions\ApplyCouponAction; class Dashboard extends Component implements HasActions { use InteractsWithActions; public function applyCouponAction(): Action { return ApplyCouponAction::make() ->button() ->label('Apply Coupon'); } public function render() { return view('dashboard'); } }
{{-- dashboard.blade.php --}} <div> <h2>Dashboard</h2> {{ $this->applyCouponAction }} </div>
In Resource Pages:
use Noxo\FilamentCoupons\Actions\ApplyCouponAction; class ListPosts extends ListRecords { protected function getHeaderActions(): array { return [ Actions\CreateAction::make(), ApplyCouponAction::make(), ]; } }
In Custom Pages:
use Noxo\FilamentCoupons\Actions\ApplyCouponAction; class SettingsPage extends Page { protected function getHeaderActions(): array { return [ ApplyCouponAction::make() ->label('Redeem Coupon') ->color('success'), ]; } }
Manual Using
$coupon = Coupon::where('code', 'WELCOME2012')->first(); // Validate if (coupons()->isValid($coupon)) { // Apply coupons()->applyCoupon($coupon); }
Testing
Run the test suite using:
composer test
The tests verify all aspects of coupon functionality, including strategy handling and integration with Filament. We recommend running tests after any changes.
Credits
License
The MIT License (MIT). Please see License File for more information.