Skip to content

Commit

Permalink
Add variant attributes to product edit
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Jan 2, 2025
1 parent 53605c6 commit 02bead9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/admin/src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ public static function getDefaultForm(Form $form): Form
static::getMainFormComponents(),
),
static::getAttributeDataFormComponent(),
Attributes::make()->using(
ProductVariant::class
)->afterStateHydrated(function ($state, ?Model $record, $component) {
$variant = $record->variants->first();
if ($variant) {
$component->state($variant->attribute_data);
}
})->visible(
fn (?Model $record) => $record && $record->variants()->count() == 1
)->statePath('variant_attributes')
])
->columns(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Actions;
use Filament\Forms;
use Filament\Support\Facades\FilamentIcon;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Actions\Products\ForceDeleteProductAction;
use Lunar\Admin\Support\Pages\BaseEditRecord;
Expand Down Expand Up @@ -62,4 +63,21 @@ public function getRelationManagers(): array
{
return [];
}

protected function handleRecordUpdate(Model $record, array $data): Model
{
$data = $this->callLunarHook('beforeUpdate', $data, $record);

$variantData = $data['variant_attributes'] ?? null;

if ($variantData) {
$variant = $record->variants()->first();
$variant->attribute_data = collect($variantData);
$variant->save();
}

$record = parent::handleRecordUpdate($record, $data);

return $this->callLunarHook('afterUpdate', $record, $data);
}
}
12 changes: 10 additions & 2 deletions packages/admin/src/Support/Forms/Components/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

class Attributes extends Forms\Components\Group
{
public ?string $modelClassOverride = null;

public function using(string $modelClass): self
{
$this->modelClassOverride = $modelClass;

return $this;
}

protected function setUp(): void
{
parent::setUp();
Expand All @@ -22,7 +31,7 @@ protected function setUp(): void

if (blank($this->childComponents)) {
$this->schema(function (\Filament\Forms\Get $get, Livewire $livewire, ?Model $record) {
$modelClass = $livewire::getResource()::getModel();
$modelClass = $this->modelClassOverride ?: $livewire::getResource()::getModel();

$productTypeId = null;

Expand Down Expand Up @@ -72,7 +81,6 @@ protected function setUp(): void
foreach ($group['fields'] as $field) {
$sectionFields[] = AttributeData::getFilamentComponent($field);
}

$groupComponents[] = Forms\Components\Section::make($group['model']->translate('name'))
->schema($sectionFields);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use Livewire\Livewire;

uses(\Lunar\Tests\Admin\Unit\Filament\TestCase::class)
->group('resource.product');

it('can edit variant attributes', function () {
\Lunar\Models\CustomerGroup::factory()->create([
'default' => true,
]);

\Lunar\Models\Language::factory()->create([
'default' => true,
]);

$product = \Lunar\Models\Product::factory()->create();
$variant = \Lunar\Models\ProductVariant::factory()->create([
'product_id' => $product->id,
]);

$group = \Lunar\Models\AttributeGroup::factory()->create([
'attributable_type' => 'product_variant',
'name' => [
'en' => 'Variant Details',
],
'handle' => 'variant_details',
'position' => 1,
]);

$attribute = \Lunar\Models\Attribute::factory()->create([
'attribute_type' => 'product_variant',
'attribute_group_id' => $group->id,
'position' => 1,
'name' => [
'en' => 'Test Attribute',
],
'handle' => 'test-attribute',
'section' => 'main',
'required' => false,
'system' => false,
'searchable' => false,
]);

\Illuminate\Support\Facades\DB::table('lunar_attributables')->insert([
'attribute_id' => $attribute->id,
'attributable_type' => 'product_variant',
'attributable_id' => $variant->id,
]);

$this->asStaff(admin: true);

$component = Livewire::test(\Lunar\Admin\Filament\Resources\ProductResource\Pages\EditProduct::class, [
'record' => $product->id,
'pageClass' => 'productEdit',
])->assertSuccessful();

expect($variant->attr($attribute->handle))->toBeNull();

$component->set('data.variant_attributes.'.$attribute->handle, new \Lunar\FieldTypes\Text('Hello'));
$component->call('save');

expect($variant->refresh()->attr($attribute->handle))->toBe('Hello');
});

0 comments on commit 02bead9

Please sign in to comment.