diff --git a/.gitignore b/.gitignore index 35e49c3..1946e1c 100755 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /public/**/filament /public/uploads /storage/*.key +/storage/media-library /vendor .env .env.backup diff --git a/app/Actions/CreateOrder.php b/app/Actions/CreateOrder.php index bb38f5b..f307fa7 100644 --- a/app/Actions/CreateOrder.php +++ b/app/Actions/CreateOrder.php @@ -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; @@ -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'], @@ -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(), ]); } diff --git a/app/Livewire/Pages/Collection/CollectionProducts.php b/app/Livewire/Pages/Collection/CollectionProducts.php index 32a547e..e9f8520 100644 --- a/app/Livewire/Pages/Collection/CollectionProducts.php +++ b/app/Livewire/Pages/Collection/CollectionProducts.php @@ -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 { diff --git a/app/Livewire/Pages/SingleProduct.php b/app/Livewire/Pages/SingleProduct.php index fb3dc7c..2a22122 100644 --- a/app/Livewire/Pages/SingleProduct.php +++ b/app/Livewire/Pages/SingleProduct.php @@ -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; @@ -14,6 +15,8 @@ final class SingleProduct extends Component { public Product $product; + public ?ProductVariant $selectedVariant = null; + #[Url(except: '')] public string $variant = ''; @@ -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] @@ -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); } diff --git a/app/Livewire/Pages/Store.php b/app/Livewire/Pages/Store.php index 9bc5f5e..14a993c 100644 --- a/app/Livewire/Pages/Store.php +++ b/app/Livewire/Pages/Store.php @@ -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); }); } diff --git a/app/Livewire/ShoppingCart.php b/app/Livewire/ShoppingCart.php index b3ba7d8..87e2bfb 100755 --- a/app/Livewire/ShoppingCart.php +++ b/app/Livewire/ShoppingCart.php @@ -4,7 +4,6 @@ namespace App\Livewire; -use App\Models\Product; use Darryldecode\Cart\CartCollection; use Darryldecode\Cart\Facades\CartFacade; use Filament\Notifications\Notification; @@ -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(); diff --git a/app/Livewire/VariantsSelector.php b/app/Livewire/VariantsSelector.php index 41f1d7b..e1473c7 100755 --- a/app/Livewire/VariantsSelector.php +++ b/app/Livewire/VariantsSelector.php @@ -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; @@ -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'); diff --git a/resources/views/components/cart/element.blade.php b/resources/views/components/cart/element.blade.php index a56fedf..8612d9c 100755 --- a/resources/views/components/cart/element.blade.php +++ b/resources/views/components/cart/element.blade.php @@ -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
- {{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, \App\Actions\ZoneSessionManager::getSession()?->currencyCode)]) }} +
+ {{ __(':qty x :price', ['qty' => $item->quantity, 'price' => shopper_money_format($item->price, current_currency())]) }}
+ + @if($item->attributes->isNotEmpty()) +{{ $price }} diff --git a/resources/views/components/cart/item.blade.php b/resources/views/components/cart/item.blade.php index b211f8f..2ef6022 100755 --- a/resources/views/components/cart/item.blade.php +++ b/resources/views/components/cart/item.blade.php @@ -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
{{ $price }}
@@ -30,7 +46,7 @@{{ __('Tax') }}
- {{ shopper_money_format(0, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }} + {{ shopper_money_format(0, currency: current_currency()) }}
{{ __('Subtotal') }}
- {{ shopper_money_format($subtotal, currency: \App\Actions\ZoneSessionManager::getSession()?->currencyCode) }} + {{ shopper_money_format($subtotal, currency: current_currency()) }}