Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bug on single product page and store page #39

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/public/**/filament
/public/uploads
/storage/*.key
/storage/media-library
/vendor
.env
.env.backup
Expand Down
5 changes: 2 additions & 3 deletions app/Actions/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Actions;

use App\Models\Product;
use Darryldecode\Cart\Facades\CartFacade;
use Illuminate\Support\Facades\Auth;
use Shopper\Core\Models\Country;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function handle(): Order
'number' => generate_number(),
'customer_id' => $customer->id,
'zone_id' => ZoneSessionManager::getSession()->zoneId,
'currency_code' => ZoneSessionManager::getSession()->currencyCode,
'currency_code' => current_currency(),
'shipping_address_id' => $shippingAddress->id,
'billing_address_id' => $billingAddress->id,
'shipping_option_id' => data_get($checkout, 'shipping_option')[0]['id'],
Expand All @@ -74,7 +73,7 @@ public function handle(): Order
'name' => $item->name,
'sku' => $item->associatedModel->sku,
'product_id' => $item->id,
'product_type' => Product::class,
'product_type' => $item->associatedModel->getMorphClass(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Pages/Collection/CollectionProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Livewire\Pages\Collection;

use App\Models\Collection;
use App\Models\Product;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use Shopper\Core\Models\Collection;

class CollectionProducts extends Component
{
Expand Down
11 changes: 10 additions & 1 deletion app/Livewire/Pages/SingleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Livewire\Pages;

use App\Models\Product;
use App\Models\ProductVariant;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
Expand All @@ -14,6 +15,8 @@ final class SingleProduct extends Component
{
public Product $product;

public ?ProductVariant $selectedVariant = null;

#[Url(except: '')]
public string $variant = '';

Expand All @@ -24,12 +27,18 @@ public function mount(): void
$this->product->load([
'brand',
'media',
'inventoryHistories',
'relatedProducts',
'prices' => function ($query) {
$query->whereRelation('currency', 'code', current_currency());
},
'prices.currency',
]);

$this->selectedVariant = ($this->variant) ? ProductVariant::query()
->with(['media'])
->where('id', $this->variant)
->firstOrFail() : null;
}

#[Computed]
Expand All @@ -41,7 +50,7 @@ public function averageRating(): float
public function render(): View
{
return view('livewire.pages.single-product', [
'selectedVariant' => null,
'selectedVariant' => $this->selectedVariant,
])
->title($this->product->name);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Livewire/Pages/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Store extends Component

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

if (count($this->selectedAttributes) > 0) {
$query = $query->whereHas('attributes', function ($query) {
$query = $query->whereHas('options', function ($query) {
$query->whereIn('attribute_value_id', $this->selectedAttributes);
});
}
Expand Down
5 changes: 1 addition & 4 deletions app/Livewire/ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Livewire;

use App\Models\Product;
use Darryldecode\Cart\CartCollection;
use Darryldecode\Cart\Facades\CartFacade;
use Filament\Notifications\Notification;
Expand Down Expand Up @@ -43,13 +42,11 @@ public function cartUpdated(): void

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

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

Expand Down
33 changes: 24 additions & 9 deletions app/Livewire/VariantsSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Livewire;

use App\Models\Product;
use App\Models\ProductVariant;
use Darryldecode\Cart\Facades\CartFacade;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
Expand All @@ -14,21 +15,35 @@ final class VariantsSelector extends Component
{
public Product $product;

public ?Product $selectedVariant = null;
public ?ProductVariant $selectedVariant = null;

public function mount(): void
{
$this->selectedVariant?->loadMissing([
'prices' => function ($query) {
$query->whereRelation('currency', 'code', current_currency());
},
'prices.currency',
]);

}

public function addToCart(): void
{
$model = $this->selectedVariant ?? $this->product;

// @phpstan-ignore-next-line
CartFacade::session(session()->getId())->add([
'id' => $this->selectedVariant ? $this->selectedVariant->id : $this->product->id,
'name' => $this->selectedVariant
? $this->product->name . ' / ' . $this->selectedVariant->name
: $this->product->name,
'price' => $this->selectedVariant && $this->selectedVariant->price_amount
? $this->selectedVariant->price_amount
: $this->product->price_amount,
'id' => $model->id,
'name' => $this->product->name,
'price' => $model->getPrice()->amount->amount,
'quantity' => 1,
'associatedModel' => $this->selectedVariant?->load('parent') ?? $this->product,
'attributes' => $this->selectedVariant
? $this->selectedVariant->values->mapWithKeys(function ($value) {
return [$value->attribute->name => $value->value];
})->toArray()
: [],
'associatedModel' => $model,
]);

$this->dispatch('cartUpdated');
Expand Down
28 changes: 23 additions & 5 deletions resources/views/components/cart/element.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@
@php
$price = shopper_money_format(
amount: $item->price * $item->quantity,
currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode
currency: current_currency(),
);

$model = $item->associatedModel instanceof \App\Models\ProductVariant ? $item->associatedModel->product : $item->associatedModel;
@endphp

<li class="flex items-start py-6 space-x-4">
<x-products.thumbnail :product="$item->associatedModel" class="size-20 border aspect-none border-primary-700" />
<div class="flex-auto space-y-1">
<h3>{{ $item->name }}</h3>
<p class="text-gray-400">
{{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, \App\Actions\ZoneSessionManager::getSession()?->currencyCode)]) }}
<div class="flex-auto">
<h3 class="font-medium font-heading text-white">
<x-link :href="route('single-product', $model)">
{{ $item->name }}
</x-link>
</h3>

<p class="mt-1 text-gray-300">
{{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, current_currency())]) }}
</p>

@if($item->attributes->isNotEmpty())
<ul class="mt-2">
@foreach($item->attributes as $name => $value)
<li class="text-xs text-white">
<span class="font-medium text-gray-400">{{ $name }}</span>:
<span>{{ $value }}</span>
</li>
@endforeach
</ul>
@endif
</div>
<p class="flex-none text-base font-medium">
{{ $price }}
Expand Down
30 changes: 23 additions & 7 deletions resources/views/components/cart/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@
@php
$price = shopper_money_format(
amount: $item->price * $item->quantity,
currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode
currency: current_currency(),
);

$model = $item->associatedModel instanceof \App\Models\ProductVariant ? $item->associatedModel->product : $item->associatedModel;
@endphp

<li class="flex py-6">
<x-products.thumbnail :product="$item->associatedModel" class="size-32 border border-gray-200 rounded-lg aspect-none" />
<div class="flex flex-col flex-1 ml-4">
<div class="flex justify-between text-base">
<h3 class="font-medium font-heading text-primary-900">
<x-link :href="route('single-product', $item->associatedModel)">
{{ $item->name }}
</x-link>
</h3>
<div>
<h3 class="font-medium font-heading text-primary-900">
<x-link :href="route('single-product', $model)">
{{ $item->name }}
</x-link>
</h3>

@if($item->attributes->isNotEmpty())
<ul>
@foreach($item->attributes as $name => $value)
<li class="text-sm">
<span class="text-gray-700 font-medium">{{ $name }}</span>:
<span class="text-gray-500">{{ $value }}</span>
</li>
@endforeach
</ul>
@endif
</div>

<p class="ml-4 text-gray-700">
{{ $price }}
</p>
Expand All @@ -30,7 +46,7 @@
<div class="flex">
<button
type="button"
wire:click="removeToCart('{{ $item->id }}')"
wire:click="removeToCart({{ $item->id }})"
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"
>
<x-untitledui-trash-03 class="size-4" aria-hidden="true" />
Expand Down
5 changes: 0 additions & 5 deletions resources/views/components/collections/card.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ class="object-cover object-center max-w-none size-full"
<h3 class="mt-4 text-base font-semibold text-gray-900 font-heading lg:text-lg">
{{ $collection->name }}
</h3>
@if ($collection->description)
<p class="mt-2 text-sm text-gray-500">
{{ $collection->description }}
</p>
@endif
</x-link>
2 changes: 1 addition & 1 deletion resources/views/livewire/components/cart-total.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
?>

<dd class="text-base">
{{ shopper_money_format(amount: $price, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }}
{{ shopper_money_format(amount: $price, currency: current_currency()) }}
</dd>
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
?>

<dd>
{{ shopper_money_format(amount: $price, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }}
{{ shopper_money_format(amount: $price, currency: current_currency()) }}
</dd>
2 changes: 1 addition & 1 deletion resources/views/livewire/components/tax-price.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
?>

<dd>
{{ shopper_money_format(amount: $price, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }}
{{ shopper_money_format(amount: $price, currency: current_currency()) }}
</dd>
17 changes: 10 additions & 7 deletions resources/views/livewire/components/variants-selector.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<form class="mt-6" wire:submit="addToCart">
@if ($product->variants->isNotEmpty())
<fieldset>
<legend class="text-sm text-gray-500">
<legend class="text-md text-gray-500">
Variants: <span class="font-bold text-sm text-gray-700">{{ $selectedVariant?->name }}</span>
</legend>
<div class="mt-4 grid grid-cols-2 gap-4 sm:grid-cols-4">
@foreach ($product->variants as $variant)
<x-link
:href="route('single-product', ['product' => $product , 'variant'=> $variant->slug])"
:href="route('single-product', ['product' => $product , 'variant'=> $variant])"
@class([
'inline-flex items-center justify-center text-gray-500 font-medium hover:text-gray-900 px-3 py-2.5 rounded-md',
'border border-gray-200',
'ring-2 ring-primary-600 ring-offset-2' => $selectedVariant && $selectedVariant->id === $variant->id
'inline-flex items-center justify-center text-gray-500 font-medium px-3 py-2.5 rounded-md border border-gray-200',
'ring-2 ring-primary-600 ring-offset-2 hover:text-gray-900' => $selectedVariant && $selectedVariant->id === $variant->id,
'border bg-neutral-100 px-2 py-1 relative z-10 cursor-not-allowed overflow-hidden bg-neutral-100
text-neutral-500 ring-1 ring-neutral-300 before:absolute before:inset-x-0 before:-z-10 before:h-px
before:-rotate-45 before:bg-neutral-300 before:transition-transform' => $variant->stock < 1 || is_null($variant->getPrice())
])
>
{{ $variant->name }}
Expand All @@ -25,9 +27,10 @@
<x-buttons.primary
type="submit"
class="max-w-xs px-8 py-3 sm:w-full"
:disabled="$product->variants->isNotEmpty() && ! $selectedVariant"
:disabled="$product->isVariant() && ! $selectedVariant || $product->isVariant() && $this->selectedVariant && $this->selectedVariant->stock < 1 ||
! $product->isVariant() && $product->stock < 1"
>
{{ $product->variants->isNotEmpty() && ! $selectedVariant ? __('Choose any variant') : __('Add to cart') }}
{{ $product->isVariant() && ! $selectedVariant ? __('Choose any variant') : __('Add to cart') }}
</x-buttons.primary>

<x-buttons.primary type="button" class="px-4">
Expand Down
6 changes: 3 additions & 3 deletions resources/views/livewire/pages/checkout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span class="sr-only">{{ shopper_setting('legal_name') }}</span>
<x-brand class="w-auto h-10 text-primary-700" aria-hidden="true" />
</x-link>
<x-link href="#" class="inline-flex items-center font-medium text-gray-600 hover:text-gray-900">
<x-link :href="route('store.products')" class="inline-flex items-center font-medium text-gray-600 hover:text-gray-900">
{{ __('Back to Shopping Cart') }}
</x-link>
</div>
Expand Down Expand Up @@ -39,7 +39,7 @@ class="px-4 pt-16 pb-10 bg-primary-950 sm:px-6 lg:col-start-2 lg:row-start-1 lg:
<div class="flex items-center justify-between">
<dt class="text-gray-300">{{ __('Subtotal') }}</dt>
<dd>
{{ shopper_money_format(amount: $subtotal, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }}
{{ shopper_money_format(amount: $subtotal, currency: current_currency()) }}
</dd>
</div>

Expand Down Expand Up @@ -125,7 +125,7 @@ class="relative px-4 py-6 bg-white sm:px-6"
<div class="flex items-center justify-between">
<dt class="text-gray-400">{{ __('SubTotal') }}</dt>
<dd>
{{ shopper_money_format(amount: $subtotal, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }}
{{ shopper_money_format(amount: $subtotal, currency: current_currency()) }}
</dd>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class="w-12 h-12 text-gray-400"
<p class="max-w-3xl mx-auto text-sm text-gray-500">
{{ __("You haven't ordered anything from us yet. Is this the day to change that?") }}
</p>
<x-buttons.primary link="/" class="px-4 text-sm">
<x-buttons.primary :href="route('store.products')" class="px-4 text-sm">
{{ __('Continue shopping') }}
</x-buttons.primary>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/pages/single-product.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="bg-white">
<div class="py-3 bg-white border-b border-gray-200 bg-opacity-80">
<x-container class="flex items-center justify-between px-4">
{{ Breadcrumbs::render('product', $product) }}
{{ Breadcrumbs::render('product', $product) }}
</x-container>
</div>

Expand Down Expand Up @@ -87,7 +87,7 @@ class="text-base font-medium text-gray-900"
</section>
@endif

<livewire:product.reviews :product="$product" />
<livewire:product.reviews :product="$product->load('ratings')" />
</x-container>
</div>
</div>
Loading
Loading