From d88b8c8a70bcca41b2eaac9a7a9204eba696480d Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 4 Oct 2025 14:13:48 +0200 Subject: [PATCH] new: keep cart when login in --- .../Controllers/Shop/Auth/LoginController.php | 2 + .../Shop/Auth/RegisterController.php | 2 + app/Repositories/Core/User/ShopCart.php | 103 +++++++++++++++++- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Shop/Auth/LoginController.php b/app/Http/Controllers/Shop/Auth/LoginController.php index 3043fdd2..51820df6 100644 --- a/app/Http/Controllers/Shop/Auth/LoginController.php +++ b/app/Http/Controllers/Shop/Auth/LoginController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Shop\Auth; use App\Http\Controllers\Controller; +use App\Repositories\Core\User\ShopCart; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -31,6 +32,7 @@ class LoginController extends Controller ]); if ($this->guard()->attempt($credentials, $request->get('remember'))) { + ShopCart::migrateGuestCartToUser(); $request->session()->regenerate(); if (back()->getTargetUrl() === route('Shop.Orders.store')) { $route = 'Shop.Orders.order'; diff --git a/app/Http/Controllers/Shop/Auth/RegisterController.php b/app/Http/Controllers/Shop/Auth/RegisterController.php index 1f879c2a..2982fb43 100644 --- a/app/Http/Controllers/Shop/Auth/RegisterController.php +++ b/app/Http/Controllers/Shop/Auth/RegisterController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Shop\Auth; use App\Http\Controllers\Controller; use App\Http\Requests\Shop\RegisterCustomer; +use App\Repositories\Core\User\ShopCart; use App\Repositories\Shop\CustomerSaleChannels; use App\Repositories\Shop\CustomerAddresses; use App\Repositories\Shop\Customers; @@ -33,6 +34,7 @@ class RegisterController extends Controller $user = $this->create($request->all()); $this->guard()->login($user); + ShopCart::migrateGuestCartToUser(); return $request->wantsJson() ? new JsonResponse([], 201) diff --git a/app/Repositories/Core/User/ShopCart.php b/app/Repositories/Core/User/ShopCart.php index 32ebe2e9..a4187f11 100644 --- a/app/Repositories/Core/User/ShopCart.php +++ b/app/Repositories/Core/User/ShopCart.php @@ -94,11 +94,106 @@ class ShopCart return self::get()->getContent(); } - public static function get() + public static function migrateGuestCartToUser($userId = null) { - $userId = Auth::guard('customer')->id(); - $sessionKey = 'cart_'.sha1(static::class . ($userId ?? 'guest')); + $userId = self::resolveUserId($userId); - return Cart::session($sessionKey); + if ($userId === null) { + return; + } + + $guestSessionKey = self::sessionKey(); + $guestItems = Cart::session($guestSessionKey)->getContent(); + + if ($guestItems->count() === 0) { + return; + } + + $userSessionKey = self::sessionKey($userId); + + foreach ($guestItems as $item) { + $existing = Cart::session($userSessionKey)->get($item->id); + + if ($existing) { + Cart::session($userSessionKey)->update($item->id, [ + 'quantity' => [ + 'relative' => false, + 'value' => $existing->quantity + $item->quantity, + ], + ]); + + continue; + } + + $itemData = [ + 'id' => $item->id, + 'name' => $item->name, + 'price' => $item->price, + 'quantity' => $item->quantity, + 'attributes' => self::extractAttributes($item), + ]; + + if (isset($item->associatedModel)) { + $itemData['associatedModel'] = $item->associatedModel; + } + + $conditions = self::extractConditions($item); + if (! empty($conditions)) { + $itemData['conditions'] = $conditions; + } + + Cart::session($userSessionKey)->add($itemData); + } + + Cart::session($guestSessionKey)->clear(); + Cart::session($userSessionKey); + } + + protected static function extractAttributes($item) + { + if (! isset($item->attributes)) { + return []; + } + + if (is_object($item->attributes) && method_exists($item->attributes, 'toArray')) { + return $item->attributes->toArray(); + } + + return (array) $item->attributes; + } + + protected static function extractConditions($item) + { + if (! isset($item->conditions)) { + return []; + } + + if (is_object($item->conditions) && method_exists($item->conditions, 'toArray')) { + return $item->conditions->toArray(); + } + + return (array) $item->conditions; + } + + protected static function resolveUserId($userId = null) + { + return $userId ?? Auth::guard('customer')->id(); + } + + protected static function sessionKey($userId = null) + { + $key = $userId ?? 'guest'; + + return 'cart_'.sha1(static::class.$key); + } + + protected static function session($userId = null) + { + return Cart::session(self::sessionKey($userId)); + } + + public static function get($userId = null) + { + return self::session(self::resolveUserId($userId)); } }