mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: rough session re-implementation
This commit is contained in:
@@ -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
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "joelwmale/laravel-cart",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.1",
|
||||
"description": "A cart implementation for Laravel",
|
||||
"keywords": [
|
||||
"laravel",
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user