diff --git a/README.md b/README.md index 409dadf..b155ac1 100644 --- a/README.md +++ b/README.md @@ -110,21 +110,17 @@ #### Round Mode ### Basic Usage -The cart has a default sessionKey that holds the cart data and stores it in the session. +The cart has a default sessionKey that holds the cart data and stores it in the session, so you can have multiple carts for multiple users. -This -also serves as a cart unique identifier which you can use to bind a cart to a specific user if you want to. +This also serves as a cart unique identifier which you can use to bind a cart to a specific user if you want to. -Make sure to call `\Cart::session($sessionKey)` before calling any other cart methods. +Make sure to call `\Cart::setSessionKey($sessionKey)` before calling any other cart methods. Usually this is not required. ```php // Binds the cart to a unique id (user id, session id, etc.) -\Cart::session(User::first()->id); - -// If you don't want to bind the cart on a project level, you can add it to each call -\Cart::session(User::first()->id)->add(1, 'Product 1', 19.99, 2, ['size' => 'large']); +\Cart::setSessionKey(User::first()->id); ``` #### Adding to the cart: **Cart::add()** diff --git a/composer.json b/composer.json index 158bb78..529de6c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "joelwmale/laravel-cart", - "version": "1.2.0", + "version": "1.2.1", "description": "A cart implementation for Laravel", "keywords": [ "laravel", diff --git a/src/Cart.php b/src/Cart.php index afc2d40..a0865a3 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -56,6 +56,16 @@ private function validateConfig($config) } } + public function getSessionKey(): string + { + return $this->session->getSessionKey(); + } + + public function setSessionKey(string $sessionKey): void + { + $this->session->setSessionKey($sessionKey); + } + /** * get instance name of the cart * diff --git a/src/CartSession.php b/src/CartSession.php index 38b11e6..69ffb37 100644 --- a/src/CartSession.php +++ b/src/CartSession.php @@ -47,6 +47,34 @@ public function isInternalSession($session) ]); } + public function getSessionKey() + { + return $this->sessionKey; + } + + public function setSessionKey(string $sessionKey) + { + // set the session key and then update the session + $this->sessionKey = $sessionKey; + + if ($this->driver == 'session') { + $session = $this->session->get($this->itemsKey); + $sessionKey = $this->session->get($this->conditionsKey); + + $this->session->forget($this->itemsKey); + $this->session->forget($this->conditionsKey); + + $this->itemsKey = $this->sessionKey . '_cart_items'; + $this->conditionsKey = $this->sessionKey . '_cart_conditions'; + + $this->session->put($this->itemsKey, $session); + $this->session->put($this->conditionsKey, $sessionKey); + } elseif ($this->driver == 'database') { + $this->session[$this->sessionId] = $sessionKey; + $this->session->save(); + } + } + public function has($key) { return $this->session->has($key) ?? $this->session; diff --git a/tests/Unit/CartTest.php b/tests/Unit/CartTest.php index d0057a9..bb6c7ca 100644 --- a/tests/Unit/CartTest.php +++ b/tests/Unit/CartTest.php @@ -689,3 +689,15 @@ expect($this->cart->getTotalQuantity())->toBeInt('Return type should be INT'); expect($this->cart->getTotalQuantity())->toEqual(12, 'Cart\'s quantity should be 4.'); }); + +test('can add to cart and then update the session key', function () { + $this->cart->add(1, 'Sample Item', 100.99, 2, []); + + expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should be 1'); + expect($this->cart->getContent()->first()['id'])->toEqual(1, 'Item added has ID of 1 so first content ID should be 1'); + + $this->cart->setSessionKey('new_session_key'); + + expect($this->cart->getSessionKey())->toEqual('new_session_key', 'Session key should be updated to new_session_key'); + expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should still be 1'); +});