Skip to content

Commit

Permalink
Support discounts specifying fields in the discount UI (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Aug 14, 2024
1 parent d5d480a commit a230b75
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/core/extending/discounts.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Discounts

## Overview
Expand Down Expand Up @@ -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
<?php

namespace App\DiscountTypes;

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

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('data.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

0 comments on commit a230b75

Please sign in to comment.