Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support discounts specifying fields in the discount UI #1653

Open
wants to merge 10 commits into
base: 1.x
Choose a base branch
from
47 changes: 47 additions & 0 deletions docs/core/extending/discounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,50 @@ 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
<?php

namespace App\DiscountTypes;

use Lunar\Admin\Base\LunarPanelDiscountInterface;
use Lunar\DiscountTypes\AbstractDiscountType;

class MyCustomDiscountType extends AbstractDiscountType implements LunarPanelDiscountInterface
{
/**
* Return the schema to use in the Lunar admin panel
*/
public function lunarPanelSchema(): array
{
return [
Forms\Components\TextInput::make('my_field')
->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;
}
}
```
21 changes: 21 additions & 0 deletions packages/admin/src/Base/LunarPanelDiscountInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Base;

interface LunarPanelDiscountInterface
{
/**
* Return the schema to use in the Lunar admin panel
*/
public function lunarPanelSchema(): array;

/**
* Mutate the model data before displaying it in the admin form.
*/
public function lunarPanelOnFill(array $data): array;

/**
* Mutate the form data before saving it to the discount model.
*/
public function lunarPanelOnSave(array $data): array;
}
14 changes: 14 additions & 0 deletions packages/admin/src/Filament/Resources/DiscountResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Str;
use Lunar\Admin\Base\LunarPanelDiscountInterface;
use Lunar\Admin\Filament\Resources\DiscountResource\Pages;
use Lunar\Admin\Filament\Resources\DiscountResource\RelationManagers\BrandLimitationRelationManager;
use Lunar\Admin\Filament\Resources\DiscountResource\RelationManagers\CollectionLimitationRelationManager;
Expand Down Expand Up @@ -57,6 +58,18 @@ public static function getNavigationGroup(): ?string

public static function getDefaultForm(Form $form): Form
{
$discountSchemas = Discounts::getTypes()->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()
Expand Down Expand Up @@ -84,6 +97,7 @@ public static function getDefaultForm(Form $form): Form
)->schema(
static::getAmountOffFormComponents()
),
...$discountSchemas,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down