mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dfd0ce761 | ||
|
|
a3161888aa | ||
|
|
4c02310bf7 |
@@ -13,7 +13,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
php: ['8.2', '8.3', '8.4']
|
php: ['8.3', '8.4', '8.5']
|
||||||
stability: [prefer-stable]
|
stability: [prefer-stable]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
@@ -110,21 +110,17 @@ #### Round Mode
|
|||||||
|
|
||||||
### Basic Usage
|
### 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
|
This also serves as a cart unique identifier which you can use to bind a cart to a specific user if you want to.
|
||||||
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.
|
Usually this is not required.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Binds the cart to a unique id (user id, session id, etc.)
|
// Binds the cart to a unique id (user id, session id, etc.)
|
||||||
\Cart::session(User::first()->id);
|
\Cart::setSessionKey(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']);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Adding to the cart: **Cart::add()**
|
#### Adding to the cart: **Cart::add()**
|
||||||
|
|||||||
+7
-7
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "joelwmale/laravel-cart",
|
"name": "joelwmale/laravel-cart",
|
||||||
"version": "1.2.0",
|
"version": "2.0.0",
|
||||||
"description": "A cart implementation for Laravel",
|
"description": "A cart implementation for Laravel",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel",
|
"laravel",
|
||||||
@@ -19,12 +19,12 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.2",
|
"php": ">=8.3",
|
||||||
"illuminate/support": "^10.0|^11.0|^12.0",
|
"illuminate/support": "^11.0|^12.0|^13.0",
|
||||||
"illuminate/auth": "^10.0|^11.0|^12.0",
|
"illuminate/auth": "^11.0|^12.0|^13.0",
|
||||||
"illuminate/container": "^10.0|^11.0|^12.0",
|
"illuminate/container": "^11.0|^12.0|^13.0",
|
||||||
"illuminate/database": "^10.0|^11.0|^12.0",
|
"illuminate/database": "^11.0|^12.0|^13.0",
|
||||||
"illuminate/validation": "^10.0|^11.0|^12.0"
|
"illuminate/validation": "^11.0|^12.0|^13.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"mockery/mockery": "^1.6",
|
"mockery/mockery": "^1.6",
|
||||||
|
|||||||
@@ -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
|
* get instance name of the cart
|
||||||
*
|
*
|
||||||
@@ -155,9 +165,31 @@ public function add(
|
|||||||
|
|
||||||
$this->currentItemId = $id;
|
$this->currentItemId = $id;
|
||||||
|
|
||||||
|
// clean up any incomplete items
|
||||||
|
$this->removeInvalidItems();
|
||||||
|
|
||||||
return $this;
|
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
|
* Update a cart item with whatever data is passed in
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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)
|
public function has($key)
|
||||||
{
|
{
|
||||||
return $this->session->has($key) ?? $this->session;
|
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())->toBeInt('Return type should be INT');
|
||||||
expect($this->cart->getTotalQuantity())->toEqual(12, 'Cart\'s quantity should be 4.');
|
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