Oct 11, 2024
0
Level 1
Facing an issue where the orderNow action always retrieves the first plan from the collection of plans instead of the specific plan related to the product being interacted with.
ViewProduct.php
<?php
use App\Models\Product;
use App\Models\Plan;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Infolists\Concerns\InteractsWithInfolists;
use Filament\Infolists\Contracts\HasInfolists;
use Filament\Infolists\Components\Infolist;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\Tabs;
use Filament\Infolists\Components\Tab;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Action;
use Livewire\Component;
class ViewProduct extends Component implements HasForms, HasInfolists
{
use InteractsWithInfolists;
use InteractsWithForms;
public function checkout(Plan $plan)
{
// ...
}
public function productInfolist(Infolist $infolist): Infolist
{
$products = Product::has('plans')
->where('is_visible', true)
->get();
return $infolist
->state(['products' => $products])
->schema([
Section::make('Products')
->schema([
RepeatableEntry::make('products')
->schema([
Section::make(fn ($state) => $state['name'])
->footerActions([
Action::make('orderNow')
->action(function (Action $action) {
$plan = $action->getComponent()->getState()['plans']->first();
if ($plan instanceof Plan) {
return $this->checkout($plan);
}
}),
])
->schema([
Tabs::make('intervals')
->tabs(function ($state) {
$intervals = collect($state['plans'])->pluck('interval')->unique();
return $intervals->map(function ($interval) use ($state) {
return Tab::make($interval->getLabel())
->schema([
// Plan details for each interval
]);
})->toArray();
}),
]),
]),
]),
]);
}
}
Please or to participate in this conversation.