Initial commit: WitShop Laravel Online-Shop
- Laravel 11, Blade, Alpine.js, Tailwind CSS - Produkt- & Kategorieverwaltung mit Varianten - Checkout (Vorkasse + Rechnung), Session-Warenkorb - Admin-Backend (Dashboard, Bestellungen, Kunden, CMS) - DSGVO-konform: Cookie-Consent, Impressum, Widerruf, AGB - PDF-Rechnung via dompdf, SMTP-Mailversand - Kundenverwaltung mit manueller Freischaltung Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\ProductVariant;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class CartService
|
||||
{
|
||||
private const SESSION_KEY = 'cart';
|
||||
|
||||
public function get(): array
|
||||
{
|
||||
return Session::get(self::SESSION_KEY, []);
|
||||
}
|
||||
|
||||
public function add(int $variantId, int $qty = 1): void
|
||||
{
|
||||
$cart = $this->get();
|
||||
$key = "v{$variantId}";
|
||||
if (isset($cart[$key])) {
|
||||
$cart[$key]['qty'] += $qty;
|
||||
} else {
|
||||
$variant = ProductVariant::with('product')->findOrFail($variantId);
|
||||
$cart[$key] = [
|
||||
'variant_id' => $variantId,
|
||||
'product_name' => $variant->product->name,
|
||||
'variant_label'=> $variant->label,
|
||||
'price' => $variant->effective_price,
|
||||
'tax_rate' => $variant->product->tax_rate,
|
||||
'qty' => $qty,
|
||||
];
|
||||
}
|
||||
Session::put(self::SESSION_KEY, $cart);
|
||||
}
|
||||
|
||||
public function update(string $key, int $qty): void
|
||||
{
|
||||
$cart = $this->get();
|
||||
if (isset($cart[$key])) {
|
||||
if ($qty <= 0) {
|
||||
unset($cart[$key]);
|
||||
} else {
|
||||
$cart[$key]['qty'] = $qty;
|
||||
}
|
||||
}
|
||||
Session::put(self::SESSION_KEY, $cart);
|
||||
}
|
||||
|
||||
public function remove(string $key): void
|
||||
{
|
||||
$cart = $this->get();
|
||||
unset($cart[$key]);
|
||||
Session::put(self::SESSION_KEY, $cart);
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
Session::forget(self::SESSION_KEY);
|
||||
}
|
||||
|
||||
public function subtotal(): float
|
||||
{
|
||||
return array_sum(array_map(fn($i) => $i['price'] * $i['qty'], $this->get()));
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return array_sum(array_column($this->get(), 'qty'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user