Skip to content

Fix Price object cast error when editing tier and customer group prices#2458

Open
krlmrr wants to merge 1 commit into
lunarphp:1.xfrom
krlmrr:fix/price-numeric-state-cast
Open

Fix Price object cast error when editing tier and customer group prices#2458
krlmrr wants to merge 1 commit into
lunarphp:1.xfrom
krlmrr:fix/price-numeric-state-cast

Conversation

@krlmrr
Copy link
Copy Markdown

@krlmrr krlmrr commented Apr 25, 2026

Summary

Fixes a regression in 1.5.0-beta.2 / beta.3 where opening the edit modal for a tier price or customer group price throws:

ErrorException: Object of class Lunar\DataTypes\Price could not be converted to float
at vendor/filament/schemas/src/Components/StateCasts/NumberStateCast.php:28

This is the same ordering bug as #2441 — Filament v4's NumberStateCast (registered by ->numeric()) runs floatval() on the raw state during hydration, before formatStateUsing is given a chance to unwrap the value. The two relation managers that bind a Lunar\DataTypes\Price object straight to a numeric TextInput were not covered by that fix.

PR #2441 solved the equivalent problem for FieldType attribute values by overriding hydrateState. For prices, the values arrive through plain record attributes, so EditAction::mutateRecordDataUsing() runs early enough to convert the Price object into its decimal value before child component hydration — which means the formatStateUsing calls can be removed entirely.

Changes

  • PriceRelationManager and CustomerGroupPricingRelationManager: add mutateRecordDataUsing on the EditAction to convert Price objects to their decimal value via a small unwrapPriceData helper.
  • Drop the now-redundant formatStateUsing callbacks on price / compare_price text inputs.
  • Add Pest feature tests that mount the edit action against a stored price and verify the form receives the expected decimal values (these tests fail on 1.x without the fix with the original exception).

Test plan

  • vendor/bin/pest --testsuite=admin --group=resource.product
  • vendor/bin/pest --testsuite=admin --filter='customer group price|tier price'
  • vendor/bin/pint --format agent on touched files
  • Confirmed the new tests fail on 1.x without the fix (reproducing the original Object of class Lunar\DataTypes\Price could not be converted to float exception)

Filament v4's `NumberStateCast` (added by `->numeric()`) calls `floatval()`
on raw state during form hydration, before `formatStateUsing` can run. The
edit form distributes the `Lunar\DataTypes\Price` object straight from the
record's attributes, so hydration throws:

    Object of class Lunar\DataTypes\Price could not be converted to float

Mirrors the ordering fix from lunarphp#2441 (which handled `FieldType` values for
attributes) but applied to the Price relation managers, where converting
the value via `mutateRecordDataUsing` is sufficient — Price values come in
through plain record attributes, so we can unwrap them before the form
fills, and the `formatStateUsing` calls become unnecessary.

Affected forms: tier price edit (`PriceRelationManager`) and customer
group pricing edit (`CustomerGroupPricingRelationManager`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant