From 6244af74f308f1459f4174f7762dc987ad73f7aa Mon Sep 17 00:00:00 2001 From: roble Date: Wed, 4 Nov 2026 21:07:03 +0000 Subject: [PATCH] fix: open products with a price for editing The price repeater's item label called Currency::from() on the row's currency, which is already the enum once loaded from a saved Price, so the edit page crashed for any product with a price. Co-Authored-By: Claude Opus 5.5 --- .../Resources/Products/Schemas/ProductForm.php | 17 +++++++++++++---- tests/Feature/ProductFormLayoutTest.php | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Filament/Resources/Products/Schemas/ProductForm.php b/src/Filament/Resources/Products/Schemas/ProductForm.php index a134a8f..d4c0608 100644 --- a/src/Filament/Resources/Products/Schemas/ProductForm.php +++ b/src/Filament/Resources/Products/Schemas/ProductForm.php @@ -45,6 +45,18 @@ private static function kindIs(mixed $state, PlanKind $kind): bool return ($state instanceof PlanKind ? $state : PlanKind::tryFrom((string) $state)) === $kind; } + /** @param array $state A price row: its currency is the enum once saved, a string while unsaved. */ + private static function priceLabel(array $state): ?string + { + if (blank($state['amount'] ?? null) || blank($state['currency'] ?? null)) { + return null; + } + + $currency = $state['currency'] instanceof Currency ? $state['currency'] : Currency::from($state['currency']); + + return $currency->formatPrice((int) $state['amount'], $state['interval'] ?? null, (int) ($state['interval_count'] ?? 1)); + } + /** * The stored `{features, limits}` as one row per entitlement. `EditProduct` * fills the form with it. @@ -329,10 +341,7 @@ private static function groups(): array ->defaultItems(0) ->columnSpanFull() ->collapsible() - ->itemLabel(fn (array $state): ?string => filled($state['amount'] ?? null) && filled($state['currency'] ?? null) - ? Currency::from($state['currency'])->formatPrice((int) $state['amount'], $state['interval'] ?? null, (int) ($state['interval_count'] ?? 1)) - : null - ), + ->itemLabel(fn (array $state): ?string => self::priceLabel($state)), ]), ]], diff --git a/tests/Feature/ProductFormLayoutTest.php b/tests/Feature/ProductFormLayoutTest.php index de70cfc..0faafcb 100644 --- a/tests/Feature/ProductFormLayoutTest.php +++ b/tests/Feature/ProductFormLayoutTest.php @@ -56,6 +56,16 @@ public function test_a_plan_is_created_through_the_wizard(): void $this->assertTrue($plan->entitlements()->allows('export data')); } + public function test_a_plan_with_a_price_opens_for_editing(): void + { + $plan = Product::factory()->create(); + $plan->prices()->create(['amount' => 900, 'currency' => 'USD', 'interval' => 'month', 'interval_count' => 1]); + + Livewire::test(EditProduct::class, ['record' => $plan->getKey()]) + ->assertOk() + ->assertSee('$9.00'); + } + public function test_the_wizard_stops_on_a_missing_name(): void { Livewire::test(CreateProduct::class)