|
8 | 8 | use Darryldecode\Cart\Facades\CartFacade; |
9 | 9 | use Filament\Notifications\Notification; |
10 | 10 | use Illuminate\Contracts\View\View; |
11 | | -use Illuminate\Http\Request; |
12 | 11 | use Livewire\Component; |
13 | 12 |
|
14 | 13 | final class VariantsSelector extends Component |
15 | 14 | { |
16 | 15 | public Product $product; |
17 | 16 |
|
18 | | - public ?string $search = null; |
19 | | - |
20 | | - public ?Product $currentVariant = null; |
21 | | - |
22 | | - public function mount(Request $request): void |
23 | | - { |
24 | | - $this->search = $request->query('variant'); |
25 | | - } |
| 17 | + public ?Product $selectedVariant = null; |
26 | 18 |
|
27 | 19 | public function addToCart(): void |
28 | 20 | { |
29 | | - $this->product->loadMissing('media'); |
30 | 21 | // @phpstan-ignore-next-line |
31 | 22 | CartFacade::session(session()->getId())->add([ |
32 | | - 'id' => (! $this->currentVariant) ? $this->product->id : $this->currentVariant->id, |
33 | | - 'name' => (! $this->currentVariant) ? $this->product->name : $this->product->name . ' / ' . $this->currentVariant->name, |
34 | | - 'price' => (! $this->currentVariant) ? $this->product->price_amount : $this->currentVariant->price_amount, |
| 23 | + 'id' => $this->selectedVariant ? $this->selectedVariant->id : $this->product->id, |
| 24 | + 'name' => $this->selectedVariant |
| 25 | + ? $this->product->name . ' / ' . $this->selectedVariant->name |
| 26 | + : $this->product->name, |
| 27 | + 'price' => $this->selectedVariant && $this->selectedVariant->price_amount |
| 28 | + ? $this->selectedVariant->price_amount |
| 29 | + : $this->product->price_amount, |
35 | 30 | 'quantity' => 1, |
36 | | - 'attributes' => (! $this->currentVariant) ? $this->product->attributes : [], |
37 | | - 'associatedModel' => (! $this->currentVariant) ? $this->product : $this->currentVariant, |
| 31 | + 'associatedModel' => $this->selectedVariant?->load('parent') ?? $this->product, |
38 | 32 | ]); |
39 | 33 |
|
40 | 34 | $this->dispatch('cartUpdated'); |
41 | 35 |
|
42 | 36 | Notification::make() |
43 | 37 | ->title(__('Cart updated')) |
44 | | - ->body(__('Product has been added to cart')) |
| 38 | + ->body(__('Product / variant has been added to your cart')) |
45 | 39 | ->success() |
46 | 40 | ->send(); |
47 | 41 | } |
|
0 commit comments