Skip to content
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
24 changes: 15 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=shopper
# DB_USERNAME=root
# DB_PASSWORD=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shopper
DB_USERNAME=root
DB_PASSWORD=

SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

CACHE_STORE=file
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1

Expand Down
16 changes: 16 additions & 0 deletions app/DTO/PriceData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\DTO;

use Shopper\Core\Helpers\Price;

final class PriceData
{
public function __construct(
public Price $amount,
public ?Price $compare,
public ?float $percentage,
) {}
}
2 changes: 1 addition & 1 deletion app/Livewire/Modals/Customer/AddressForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Actions\CountriesWithZone;
use App\Actions\ZoneSessionManager;
use App\Models\Country;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
Expand All @@ -15,6 +14,7 @@
use LivewireUI\Modal\ModalComponent;
use Shopper\Core\Enum\AddressType;
use Shopper\Core\Models\Address;
use Shopper\Core\Models\Country;

final class AddressForm extends ModalComponent
{
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Modals/ZoneSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function selectZone(int $countryId): void

if ($selectedZone->countryId !== ZoneSessionManager::getSession()?->countryId) {
ZoneSessionManager::setSession($selectedZone);

session()->forget('checkout');

$this->dispatch('zoneChanged');
Expand Down
11 changes: 10 additions & 1 deletion app/Livewire/Pages/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ final class Home extends Component
public function render(): View
{
return view('livewire.pages.home', [
'products' => Product::with(['brand', 'media'])
'products' => Product::query()
->select('id', 'name', 'slug', 'brand_id')
->with([
'brand',
'media',
'prices' => function ($query) {
$query->whereRelation('currency', 'code', current_currency());
},
'prices.currency',
])
->scopes('publish')
->limit(8)
->get(),
Expand Down
20 changes: 10 additions & 10 deletions app/Livewire/Pages/SingleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ final class SingleProduct extends Component
#[Url(except: '')]
public string $variant = '';

public function mount(Product $product): void
public function mount(): void
{
abort_unless($product->isPublished(), 404);
abort_unless($this->product->isPublished(), 404);

$this->product = $product->load([
$this->product->load([
'brand',
'media',
'relatedProducts',
'variants',
'variants.media',
'prices' => function ($query) {
$query->whereRelation('currency', 'code', current_currency());
},
'prices.currency',
]);
}

#[Computed]
public function getAverageRatingProperty(): float
public function averageRating(): float
{
return floatval($this->product->averageRating(1)->first());
}

public function render(): View
{
return view('livewire.pages.single-product', [
'selectedVariant' => Product::with('media')
->where('slug', $this->variant)
->select('name', 'slug', 'sku', 'id', 'price_amount', 'old_price_amount')
->first(),
'selectedVariant' => null,
])
->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,8 +21,8 @@ class Store extends Component

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

if (count($this->selectedAttributes) > 0) {
Expand Down
16 changes: 0 additions & 16 deletions app/Models/Country.php

This file was deleted.

19 changes: 3 additions & 16 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,15 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use App\Traits\HasProductPricing;
use Shopper\Core\Models\Product as Model;

final class Product extends Model
{
use HasProductPricing;

public function isPublished(): bool
{
return $this->is_visible && $this->published_at && $this->published_at <= now();
}

public function discountPercentage(): Attribute
{
return Attribute::make(
get: fn () => $this->old_price_amount > 0
? round((($this->old_price_amount - $this->price_amount) / $this->old_price_amount) * 100)
: 0
);
}

public function scopeParent(Builder $query): void
{
$query->whereNull('parent_id');
}
}
13 changes: 13 additions & 0 deletions app/Models/ProductVariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Models;

use App\Traits\HasProductPricing;
use Shopper\Core\Models\ProductVariant as Model;

final class ProductVariant extends Model
{
use HasProductPricing;
}
3 changes: 2 additions & 1 deletion app/Providers/LivewireStarterKitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Providers;

use App\View\Composers\CategoriesNavigation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

Expand All @@ -17,7 +18,7 @@ public function register(): void

public function boot(): void
{
//
Model::preventLazyLoading();
}

private function loadViewsComposer(): void
Expand Down
37 changes: 37 additions & 0 deletions app/Traits/HasProductPricing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Traits;

use App\DTO\PriceData;
use Shopper\Core\Helpers\Price;

trait HasProductPricing
{
public function getPrice(): ?PriceData
{
$currencyCode = current_currency();

$price = $this->prices
->map(function ($price) use ($currencyCode) {
$price->amount = is_no_division_currency($currencyCode)
? $price->amount
: $price->amount * 100;

return $price;
})
->reject(fn ($price) => $price->currency->code !== $currencyCode)
->first();

return $price
? new PriceData(
amount: Price::from($price->amount, $currencyCode),
compare: $price->compare_amount ? Price::from($price->compare_amount, $currencyCode) : null,
percentage: $price->compare_amount > 0
? round((($price->compare_amount - $price->amount) / $price->compare_amount) * 100)
: null
)
: null;
}
}
2 changes: 1 addition & 1 deletion app/Traits/HasProductRatings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait HasProductRatings

public ?Collection $reviews = null;

public function loadProductRatings(Product $product, int $limit = 3): void
public function loadProductRatings(Product $product): void
{
// @phpstan-ignore-next-line
$this->averageRating = floatval($product->averageRating(1)->first()) ?? $this->averageRating;
Expand Down
14 changes: 14 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use App\Actions\ZoneSessionManager;

if (! function_exists('current_currency')) {
function current_currency(): string
{
return ZoneSessionManager::checkSession()
? ZoneSessionManager::getSession()->currencyCode
: shopper_currency();
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": []
"files": [
"app/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
Loading
Loading