From 3d2f1c5c44fd83b73d633082d618e1f6356d018e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C3=96zdemir?= Date: Thu, 31 Aug 2023 23:49:42 +0300 Subject: [PATCH] add cookie keyy --- src/Cart.php | 13 ++++++++++++- tests/CartTest.php | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 1cb12f2..77ccc2d 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -3,6 +3,7 @@ namespace Ozdemir\Aurora; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Cookie; use Ozdemir\Aurora\Storage\StorageInterface; class Cart implements \Serializable @@ -417,7 +418,17 @@ public function updateConditionStorage() public static function defaultSessionKey(): string { - return Auth::check() ? Auth::id() : session()->getId(); + if (Auth::check()) { + return 'user:' . Auth::id(); + } + $guestToken = Cookie::get('guest_token'); + + if (!$guestToken) { + $guestToken = uniqid(); + Cookie::queue('guest_token', $guestToken, 1440); + } + + return 'guest:' . $guestToken; } public function loadSession($key) diff --git a/tests/CartTest.php b/tests/CartTest.php index bab30bb..e580fc4 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -430,7 +430,7 @@ expect(Cart::quantity())->toBe(2); expect(Cart::weight())->toBe(8); expect(Cart::getInstanceKey())->toBe('cart'); - expect(Cart::getSessionKey())->toBe(session()->getId()); + expect(Cart::getSessionKey())->toContain('guest:'); $user = (new \Illuminate\Foundation\Auth\User())->forceFill([ 'id' => 123,