最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - FilamentPHP: total_price is 0 when creating an order, only updates on edit - Stack Overflow

programmeradmin1浏览0评论

I'm building an order management system using Laravel Filament. I have an OrderResource where the total_price should be calculated based on the selected menus and packages.

However, I'm facing an issue:

When I create a new order, total_price remains 0. But when I edit the order and save it without making any changes, the total_price gets updated correctly. I'm using a Repeater field to allow selecting menus and packages, and I try to update the subtotal and total price using afterStateUpdated(). Here's the relevant part of my code:

Menu Repeater:

Repeater::make('items')
    ->relationship('items')
    ->schema([
        Grid::make(3)
            ->schema([
                Select::make('menu_id')
                    ->relationship('menu', 'name')
                    ->required()
                    ->label('Menu')
                    ->live()
                    ->afterStateUpdated(function ($state, $set, $get) {
                        self::updateSubtotal($state, $set, $get, 'menu');
                    })
                    ->searchable()
                    ->preload(),

                TextInput::make('quantity')
                    ->numeric()
                    ->required()
                    ->default(1)
                    ->live()
                    ->afterStateUpdated(function ($state, $set, $get) {
                        self::updateSubtotal($get('menu_id'), $set, $get, 'menu');
                    }),

                TextInput::make('subtotal')
                    ->numeric()
                    ->prefix('Rp')
                    ->disabled()
                    ->dehydrated()
                    ->label('Subtotal'),
            ]),
    ])
    ->label('Selected Menus')
    ->collapsible()
    ->defaultItems(1)
    ->addActionLabel('Add Menu');

Subtotal & Total Price Calculation:

private static function updateSubtotal($id, $set, $get, $type)
{
    $model = $type === 'menu' ? Menu::class : Package::class;
    $price = $id ? $model::find($id)?->price ?? 0 : 0;
    $quantity = $get('quantity') ?? 0;
    $subtotal = $price * $quantity;
    $set('subtotal', $subtotal);

    // Calculate total price
    $items = $get('../../items') ?? [];
    $packages = $get('../../packages') ?? [];

    $totalItems = array_reduce($items, fn ($carry, $item) => $carry + ($item['subtotal'] ?? 0), 0);
    $totalPackages = array_reduce($packages, fn ($carry, $package) => $carry + ($package['subtotal'] ?? 0), 0);

    $set('../../total_price', $totalItems + $totalPackages);
}

Issue Summary: total_price is 0 when creating a new order. total_price is only updated if I edit the order and save it again. I want total_price to be calculated immediately upon order creation.

When I create a new order and select a menu/package, the total_price should be calculated and displayed immediately. I should not have to edit and save the order for total_price to be updated.

发布评论

评论列表(0)

  1. 暂无评论