From a230b75a4cc468ea7f5c62e754efe2a25198e2a8 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 14 Aug 2024 09:38:58 +0100 Subject: [PATCH] Support discounts specifying fields in the discount UI (#1653) --- docs/core/extending/discounts.md | 49 +++++++++++++++++++ .../src/Base/LunarPanelDiscountInterface.php | 21 ++++++++ .../Filament/Resources/DiscountResource.php | 14 ++++++ .../DiscountResource/Pages/EditDiscount.php | 22 +++++++++ 4 files changed, 106 insertions(+) create mode 100644 packages/admin/src/Base/LunarPanelDiscountInterface.php diff --git a/docs/core/extending/discounts.md b/docs/core/extending/discounts.md index f23987d199..3e98b70239 100644 --- a/docs/core/extending/discounts.md +++ b/docs/core/extending/discounts.md @@ -1,3 +1,4 @@ + # Discounts ## Overview @@ -45,3 +46,51 @@ class MyCustomDiscountType extends AbstractDiscountType } } ``` + + +## Adding form fields for your discount in the admin panel + +If you require fields in the Lunar admin for your discount type, ensure your discount implements `Lunar\Admin\Base\LunarPanelDiscountInterface`. You will need to provide the `lunarPanelSchema`, `lunarPanelOnFill` and `lunarPanelOnSave` methods. + +```php +label('My label') + ->required(), + ]; + } + + /** + * Mutate the model data before displaying it in the admin form. + */ + public function lunarPanelOnFill(array $data): array + { + // optionally do something with $data + return $data; + } + + /** + * Mutate the form data before saving it to the discount model. + */ + public function lunarPanelOnSave(array $data): array + { + // optionally do something with $data + return $data; + } +} +``` diff --git a/packages/admin/src/Base/LunarPanelDiscountInterface.php b/packages/admin/src/Base/LunarPanelDiscountInterface.php new file mode 100644 index 0000000000..936950cdee --- /dev/null +++ b/packages/admin/src/Base/LunarPanelDiscountInterface.php @@ -0,0 +1,21 @@ +map(function ($discount) { + if (! $discount instanceof LunarPanelDiscountInterface) { + return; + } + + return Forms\Components\Section::make(Str::slug(get_class($discount))) + ->heading($discount->getName()) + ->visible( + fn (Forms\Get $get) => $get('type') == get_class($discount) + )->schema($discount->lunarPanelSchema()); + })->filter(); + return $form->schema([ Forms\Components\Section::make('')->schema( static::getMainFormComponents() @@ -84,6 +97,7 @@ public static function getDefaultForm(Form $form): Form )->schema( static::getAmountOffFormComponents() ), + ...$discountSchemas, ]); } diff --git a/packages/admin/src/Filament/Resources/DiscountResource/Pages/EditDiscount.php b/packages/admin/src/Filament/Resources/DiscountResource/Pages/EditDiscount.php index 754c8fe12b..6859259c9d 100644 --- a/packages/admin/src/Filament/Resources/DiscountResource/Pages/EditDiscount.php +++ b/packages/admin/src/Filament/Resources/DiscountResource/Pages/EditDiscount.php @@ -3,6 +3,7 @@ namespace Lunar\Admin\Filament\Resources\DiscountResource\Pages; use Filament\Actions; +use Lunar\Admin\Base\LunarPanelDiscountInterface; use Lunar\Admin\Filament\Resources\DiscountResource; use Lunar\Admin\Support\Pages\BaseEditRecord; use Lunar\DiscountTypes\BuyXGetY; @@ -19,8 +20,29 @@ protected function getDefaultHeaderActions(): array ]; } + protected function mutateFormDataBeforeFill(array $data): array + { + if (class_exists($data['type'])) { + $type = new $data['type']; + + if ($type instanceof LunarPanelDiscountInterface) { + return $type->lunarPanelOnFill($data); + } + } + + return $data; + } + protected function mutateFormDataBeforeSave(array $data): array { + if (class_exists($data['type'])) { + $type = new $data['type']; + + if ($type instanceof LunarPanelDiscountInterface) { + return $type->lunarPanelOnSave($data); + } + } + $minPrices = $data['data']['min_prices'] ?? []; $fixedPrices = $data['data']['fixed_values'] ?? []; $currencies = Currency::enabled()->get();