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,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ShippingRate;
|
||||
use App\Services\CartService;
|
||||
use App\Services\OrderService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CheckoutController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private CartService $cart,
|
||||
private OrderService $orderService,
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
if ($this->cart->count() === 0) {
|
||||
return redirect()->route('cart.index')->with('error', 'Ihr Warenkorb ist leer.');
|
||||
}
|
||||
$subtotal = $this->cart->subtotal();
|
||||
$shippingRate = ShippingRate::forCart($subtotal);
|
||||
$shippingCost = $shippingRate ? $shippingRate->getCostForTotal($subtotal) : 0;
|
||||
$total = $subtotal + $shippingCost;
|
||||
$userAddresses = auth()->user()?->addresses ?? collect();
|
||||
|
||||
return view('shop.checkout.index', compact('subtotal', 'shippingCost', 'total', 'userAddresses'));
|
||||
}
|
||||
|
||||
public function storeAddress(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'billing.first_name' => 'required|string|max:100',
|
||||
'billing.last_name' => 'required|string|max:100',
|
||||
'billing.street' => 'required|string|max:255',
|
||||
'billing.zip' => 'required|string|max:10',
|
||||
'billing.city' => 'required|string|max:100',
|
||||
'billing.country' => 'required|string|max:2',
|
||||
'payment_method' => 'required|in:prepayment,invoice',
|
||||
'agb' => 'accepted',
|
||||
];
|
||||
|
||||
if (!auth()->check()) {
|
||||
$rules['email'] = 'required|email';
|
||||
}
|
||||
|
||||
if ($request->input('shipping_same') !== '1') {
|
||||
$rules['shipping.first_name'] = 'required|string|max:100';
|
||||
$rules['shipping.last_name'] = 'required|string|max:100';
|
||||
$rules['shipping.street'] = 'required|string|max:255';
|
||||
$rules['shipping.zip'] = 'required|string|max:10';
|
||||
$rules['shipping.city'] = 'required|string|max:100';
|
||||
$rules['shipping.country'] = 'required|string|max:2';
|
||||
}
|
||||
|
||||
$data = $request->validate($rules);
|
||||
|
||||
if ($request->input('shipping_same') === '1') {
|
||||
$data['shipping'] = $data['billing'];
|
||||
}
|
||||
|
||||
$data['email'] = $request->input('email');
|
||||
|
||||
session(['checkout_data' => $data]);
|
||||
|
||||
return redirect()->route('checkout.confirm');
|
||||
}
|
||||
|
||||
public function payment()
|
||||
{
|
||||
return redirect()->route('checkout.index');
|
||||
}
|
||||
|
||||
public function confirm(Request $request)
|
||||
{
|
||||
$checkoutData = session('checkout_data');
|
||||
if (!$checkoutData || $this->cart->count() === 0) {
|
||||
return redirect()->route('checkout.index');
|
||||
}
|
||||
|
||||
if ($request->isMethod('get')) {
|
||||
$items = $this->cart->get();
|
||||
$subtotal = $this->cart->subtotal();
|
||||
$shippingRate = ShippingRate::forCart($subtotal);
|
||||
$shippingCost = $shippingRate ? $shippingRate->getCostForTotal($subtotal) : 0;
|
||||
$total = $subtotal + $shippingCost;
|
||||
return view('shop.checkout.confirm', compact('checkoutData', 'items', 'subtotal', 'shippingCost', 'total'));
|
||||
}
|
||||
|
||||
$order = $this->orderService->createFromSession(
|
||||
$checkoutData,
|
||||
$checkoutData['payment_method']
|
||||
);
|
||||
|
||||
session()->forget('checkout_data');
|
||||
|
||||
return redirect()->route('checkout.thankyou', $order->order_number);
|
||||
}
|
||||
|
||||
public function thankyou(string $order)
|
||||
{
|
||||
$order = \App\Models\Order::where('order_number', $order)->firstOrFail();
|
||||
return view('shop.checkout.thankyou', compact('order'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user