Skip to content

Commit 1486790

Browse files
fix: fix bug on single product page and store page
1 parent 8b7a768 commit 1486790

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/public/**/filament
88
/public/uploads
99
/storage/*.key
10+
/storage/media-library
1011
/vendor
1112
.env
1213
.env.backup

app/Livewire/Pages/SingleProduct.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Livewire\Pages;
66

77
use App\Models\Product;
8+
use App\Models\ProductVariant;
89
use Illuminate\Contracts\View\View;
910
use Livewire\Attributes\Computed;
1011
use Livewire\Attributes\Url;
@@ -14,13 +15,14 @@ final class SingleProduct extends Component
1415
{
1516
public Product $product;
1617

18+
public ?ProductVariant $selectedVariant = null;
19+
1720
#[Url(except: '')]
1821
public string $variant = '';
1922

2023
public function mount(): void
2124
{
2225
abort_unless($this->product->isPublished(), 404);
23-
2426
$this->product->load([
2527
'brand',
2628
'media',
@@ -30,6 +32,11 @@ public function mount(): void
3032
},
3133
'prices.currency',
3234
]);
35+
36+
$this->selectedVariant = ($this->variant) ? ProductVariant::query()
37+
->with(['media'])
38+
->where('id', $this->variant)
39+
->firstOrFail() : null;
3340
}
3441

3542
#[Computed]
@@ -41,7 +48,7 @@ public function averageRating(): float
4148
public function render(): View
4249
{
4350
return view('livewire.pages.single-product', [
44-
'selectedVariant' => null,
51+
'selectedVariant' => $this->selectedVariant,
4552
])
4653
->title($this->product->name);
4754
}

app/Livewire/Pages/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Store extends Component
2121

2222
public function render(): View
2323
{
24-
$query = Product::with(['media', 'attributes', 'prices', 'prices.currency'])
24+
$query = Product::with(['media', 'options', 'prices', 'prices.currency'])
2525
->scopes(['publish'])
2626
->latest();
2727

app/Livewire/VariantsSelector.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Livewire;
66

77
use App\Models\Product;
8+
use App\Models\ProductVariant;
89
use Darryldecode\Cart\Facades\CartFacade;
910
use Filament\Notifications\Notification;
1011
use Illuminate\Contracts\View\View;
@@ -14,21 +15,31 @@ final class VariantsSelector extends Component
1415
{
1516
public Product $product;
1617

17-
public ?Product $selectedVariant = null;
18+
public ?ProductVariant $selectedVariant = null;
19+
20+
public function mount(): void
21+
{
22+
$this->selectedVariant?->load([
23+
'prices' => function ($query) {
24+
$query->whereRelation('currency', 'code', current_currency());
25+
},
26+
'prices.currency',
27+
]);
28+
}
1829

1930
public function addToCart(): void
2031
{
32+
$price = $this->selectedVariant && $this->selectedVariant->getPrice()
33+
? $this->selectedVariant->getPrice()->amount->amount
34+
: $this->product->getPrice()->amount->amount;
35+
2136
// @phpstan-ignore-next-line
2237
CartFacade::session(session()->getId())->add([
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,
38+
'id' => $this->product->id,
39+
'name' => $this->product->name,
40+
'price' => $price,
3041
'quantity' => 1,
31-
'associatedModel' => $this->selectedVariant?->load('parent') ?? $this->product,
42+
'associatedModel' => $this->selectedVariant ?? $this->product,
3243
]);
3344

3445
$this->dispatch('cartUpdated');

resources/views/livewire/components/variants-selector.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="mt-4 grid grid-cols-2 gap-4 sm:grid-cols-4">
88
@foreach ($product->variants as $variant)
99
<x-link
10-
:href="route('single-product', ['product' => $product , 'variant'=> $variant->slug])"
10+
:href="route('single-product', ['product' => $product , 'variant'=> $variant])"
1111
@class([
1212
'inline-flex items-center justify-center text-gray-500 font-medium hover:text-gray-900 px-3 py-2.5 rounded-md',
1313
'border border-gray-200',

0 commit comments

Comments
 (0)