Skip to content

Commit c91b9a8

Browse files
fix: fix bug on single product page and store page (#39)
* feat: update single product page and checkout page
1 parent 8b7a768 commit c91b9a8

File tree

19 files changed

+116
-57
lines changed

19 files changed

+116
-57
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/Actions/CreateOrder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace App\Actions;
66

7-
use App\Models\Product;
87
use Darryldecode\Cart\Facades\CartFacade;
98
use Illuminate\Support\Facades\Auth;
109
use Shopper\Core\Models\Country;
@@ -58,7 +57,7 @@ public function handle(): Order
5857
'number' => generate_number(),
5958
'customer_id' => $customer->id,
6059
'zone_id' => ZoneSessionManager::getSession()->zoneId,
61-
'currency_code' => ZoneSessionManager::getSession()->currencyCode,
60+
'currency_code' => current_currency(),
6261
'shipping_address_id' => $shippingAddress->id,
6362
'billing_address_id' => $billingAddress->id,
6463
'shipping_option_id' => data_get($checkout, 'shipping_option')[0]['id'],
@@ -74,7 +73,7 @@ public function handle(): Order
7473
'name' => $item->name,
7574
'sku' => $item->associatedModel->sku,
7675
'product_id' => $item->id,
77-
'product_type' => Product::class,
76+
'product_type' => $item->associatedModel->getMorphClass(),
7877
]);
7978
}
8079

app/Livewire/Pages/Collection/CollectionProducts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace App\Livewire\Pages\Collection;
66

7-
use App\Models\Collection;
87
use App\Models\Product;
98
use Illuminate\Contracts\View\View;
109
use Livewire\Component;
10+
use Shopper\Core\Models\Collection;
1111

1212
class CollectionProducts extends Component
1313
{

app/Livewire/Pages/SingleProduct.php

Lines changed: 10 additions & 1 deletion
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,6 +15,8 @@ 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

@@ -24,12 +27,18 @@ public function mount(): void
2427
$this->product->load([
2528
'brand',
2629
'media',
30+
'inventoryHistories',
2731
'relatedProducts',
2832
'prices' => function ($query) {
2933
$query->whereRelation('currency', 'code', current_currency());
3034
},
3135
'prices.currency',
3236
]);
37+
38+
$this->selectedVariant = ($this->variant) ? ProductVariant::query()
39+
->with(['media'])
40+
->where('id', $this->variant)
41+
->firstOrFail() : null;
3342
}
3443

3544
#[Computed]
@@ -41,7 +50,7 @@ public function averageRating(): float
4150
public function render(): View
4251
{
4352
return view('livewire.pages.single-product', [
44-
'selectedVariant' => null,
53+
'selectedVariant' => $this->selectedVariant,
4554
])
4655
->title($this->product->name);
4756
}

app/Livewire/Pages/Store.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ 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

2828
if (count($this->selectedAttributes) > 0) {
29-
$query = $query->whereHas('attributes', function ($query) {
29+
$query = $query->whereHas('options', function ($query) {
3030
$query->whereIn('attribute_value_id', $this->selectedAttributes);
3131
});
3232
}

app/Livewire/ShoppingCart.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace App\Livewire;
66

7-
use App\Models\Product;
87
use Darryldecode\Cart\CartCollection;
98
use Darryldecode\Cart\Facades\CartFacade;
109
use Filament\Notifications\Notification;
@@ -43,13 +42,11 @@ public function cartUpdated(): void
4342

4443
public function removeToCart(int $id): void
4544
{
46-
/** @var Product $product */
47-
$product = Product::query()->find($id);
4845
CartFacade::session($this->sessionKey)->remove($id); // @phpstan-ignore-line
4946

5047
Notification::make()
5148
->title(__('Cart updated'))
52-
->body(__('The product ":name" has been removed from your cart !', ['name' => $product->name]))
49+
->body(__('The product has been removed from your cart !'))
5350
->success()
5451
->send();
5552

app/Livewire/VariantsSelector.php

Lines changed: 24 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,35 @@ 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?->loadMissing([
23+
'prices' => function ($query) {
24+
$query->whereRelation('currency', 'code', current_currency());
25+
},
26+
'prices.currency',
27+
]);
28+
29+
}
1830

1931
public function addToCart(): void
2032
{
33+
$model = $this->selectedVariant ?? $this->product;
34+
2135
// @phpstan-ignore-next-line
2236
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,
37+
'id' => $model->id,
38+
'name' => $this->product->name,
39+
'price' => $model->getPrice()->amount->amount,
3040
'quantity' => 1,
31-
'associatedModel' => $this->selectedVariant?->load('parent') ?? $this->product,
41+
'attributes' => $this->selectedVariant
42+
? $this->selectedVariant->values->mapWithKeys(function ($value) {
43+
return [$value->attribute->name => $value->value];
44+
})->toArray()
45+
: [],
46+
'associatedModel' => $model,
3247
]);
3348

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

resources/views/components/cart/element.blade.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
@php
44
$price = shopper_money_format(
55
amount: $item->price * $item->quantity,
6-
currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode
6+
currency: current_currency(),
77
);
8+
9+
$model = $item->associatedModel instanceof \App\Models\ProductVariant ? $item->associatedModel->product : $item->associatedModel;
810
@endphp
911

1012
<li class="flex items-start py-6 space-x-4">
1113
<x-products.thumbnail :product="$item->associatedModel" class="size-20 border aspect-none border-primary-700" />
12-
<div class="flex-auto space-y-1">
13-
<h3>{{ $item->name }}</h3>
14-
<p class="text-gray-400">
15-
{{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, \App\Actions\ZoneSessionManager::getSession()?->currencyCode)]) }}
14+
<div class="flex-auto">
15+
<h3 class="font-medium font-heading text-white">
16+
<x-link :href="route('single-product', $model)">
17+
{{ $item->name }}
18+
</x-link>
19+
</h3>
20+
21+
<p class="mt-1 text-gray-300">
22+
{{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, current_currency())]) }}
1623
</p>
24+
25+
@if($item->attributes->isNotEmpty())
26+
<ul class="mt-2">
27+
@foreach($item->attributes as $name => $value)
28+
<li class="text-xs text-white">
29+
<span class="font-medium text-gray-400">{{ $name }}</span>:
30+
<span>{{ $value }}</span>
31+
</li>
32+
@endforeach
33+
</ul>
34+
@endif
1735
</div>
1836
<p class="flex-none text-base font-medium">
1937
{{ $price }}

resources/views/components/cart/item.blade.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,35 @@
55
@php
66
$price = shopper_money_format(
77
amount: $item->price * $item->quantity,
8-
currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode
8+
currency: current_currency(),
99
);
10+
11+
$model = $item->associatedModel instanceof \App\Models\ProductVariant ? $item->associatedModel->product : $item->associatedModel;
1012
@endphp
1113

1214
<li class="flex py-6">
1315
<x-products.thumbnail :product="$item->associatedModel" class="size-32 border border-gray-200 rounded-lg aspect-none" />
1416
<div class="flex flex-col flex-1 ml-4">
1517
<div class="flex justify-between text-base">
16-
<h3 class="font-medium font-heading text-primary-900">
17-
<x-link :href="route('single-product', $item->associatedModel)">
18-
{{ $item->name }}
19-
</x-link>
20-
</h3>
18+
<div>
19+
<h3 class="font-medium font-heading text-primary-900">
20+
<x-link :href="route('single-product', $model)">
21+
{{ $item->name }}
22+
</x-link>
23+
</h3>
24+
25+
@if($item->attributes->isNotEmpty())
26+
<ul>
27+
@foreach($item->attributes as $name => $value)
28+
<li class="text-sm">
29+
<span class="text-gray-700 font-medium">{{ $name }}</span>:
30+
<span class="text-gray-500">{{ $value }}</span>
31+
</li>
32+
@endforeach
33+
</ul>
34+
@endif
35+
</div>
36+
2137
<p class="ml-4 text-gray-700">
2238
{{ $price }}
2339
</p>
@@ -30,7 +46,7 @@
3046
<div class="flex">
3147
<button
3248
type="button"
33-
wire:click="removeToCart('{{ $item->id }}')"
49+
wire:click="removeToCart({{ $item->id }})"
3450
class="inline-flex items-center px-2 py-1.5 bg-red-50 rounded-md text-xs gap-2 font-medium text-red-700 hover:bg-red-100"
3551
>
3652
<x-untitledui-trash-03 class="size-4" aria-hidden="true" />

resources/views/components/collections/card.blade.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,4 @@ class="object-cover object-center max-w-none size-full"
1313
<h3 class="mt-4 text-base font-semibold text-gray-900 font-heading lg:text-lg">
1414
{{ $collection->name }}
1515
</h3>
16-
@if ($collection->description)
17-
<p class="mt-2 text-sm text-gray-500">
18-
{{ $collection->description }}
19-
</p>
20-
@endif
2116
</x-link>

0 commit comments

Comments
 (0)