Compare commits

..
3 Commits
6 changed files with 88 additions and 20 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ['8.2', '8.3', '8.4']
php: ['8.3', '8.4', '8.5']
stability: [prefer-stable]
steps:
+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()**
+11 -11
View File
@@ -1,6 +1,6 @@
{
"name": "joelwmale/laravel-cart",
"version": "1.2.0",
"version": "2.0.0",
"description": "A cart implementation for Laravel",
"keywords": [
"laravel",
@@ -19,12 +19,12 @@
}
],
"require": {
"php": ">=8.2",
"illuminate/support": "^10.0|^11.0|^12.0",
"illuminate/auth": "^10.0|^11.0|^12.0",
"illuminate/container": "^10.0|^11.0|^12.0",
"illuminate/database": "^10.0|^11.0|^12.0",
"illuminate/validation": "^10.0|^11.0|^12.0"
"php": ">=8.3",
"illuminate/support": "^11.0|^12.0|^13.0",
"illuminate/auth": "^11.0|^12.0|^13.0",
"illuminate/container": "^11.0|^12.0|^13.0",
"illuminate/database": "^11.0|^12.0|^13.0",
"illuminate/validation": "^11.0|^12.0|^13.0"
},
"require-dev": {
"mockery/mockery": "^1.6",
@@ -38,10 +38,10 @@
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"extra": {
"laravel": {
+32
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
*
@@ -155,9 +165,31 @@ public function add(
$this->currentItemId = $id;
// clean up any incomplete items
$this->removeInvalidItems();
return $this;
}
/**
* Remove any incomplete items
*/
public function removeInvalidItems(): bool
{
$carts = $this->getContent();
foreach ($carts as $index => $cart) {
if (! (isset($cart['id'], $cart['name'], $cart['quantity']))) {
// remove item
$this->remove($index);
}
}
return true;
}
/**
* Update a cart item with whatever data is passed in
*/
+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');
});