Compare commits

..
1 Commits
Author SHA1 Message Date
Joel Male 4c02310bf7 feat: rough session re-implementation 2025-04-14 16:53:35 +10:00
5 changed files with 55 additions and 9 deletions
+4 -8
View File
@@ -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()**
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "joelwmale/laravel-cart",
"version": "1.2.0",
"version": "1.2.1",
"description": "A cart implementation for Laravel",
"keywords": [
"laravel",
+10
View File
@@ -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
*
+28
View File
@@ -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;
+12
View File
@@ -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');
});