Skip to content

Commit 0d09357

Browse files
committed
fix bug
1 parent e21ef1a commit 0d09357

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/CartService.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ class CartService
5151
*/
5252
protected $sessionStatus;
5353

54+
/**
55+
* @throws \Exception
56+
*/
5457
public function __construct($instanceName, $storage, $connection, $user = null, $sessionStatus = true)
5558
{
5659
$this->instanceName = $instanceName;
5760
$this->storage = $storage;
5861
$this->connection = $connection;
5962
$this->user = $user;
6063
$this->sessionStatus = $sessionStatus;
61-
if ($this->connection == 'database' && is_null($this->user)) {
62-
throw new \Exception('user is required');
63-
}
64-
$this->cart = $this->connection == 'database'
64+
$session = $this->storage->get($this->instanceName) ?? collect([]);
65+
$cart = $this->connection == 'database'
6566
? $this->storage->all()->where('user_id', $this->user->id)->mapWithKeys(function ($item) {
6667
return [$item['rowId'] => [
6768
'id' => $item['rowId'],
@@ -70,8 +71,12 @@ public function __construct($instanceName, $storage, $connection, $user = null,
7071
'cartable_id' => $item['cartable_id'],
7172
'cartable_type' => $item['cartable_type'],
7273
]];
73-
})
74-
: $this->storage->get($this->instanceName) ?? collect([]);
74+
})->toArray()
75+
: $session->toArray();
76+
$this->cart=collect($cart);
77+
if ($this->connection == 'database' && is_null($this->user)) {
78+
throw new \Exception('user is required');
79+
}
7580
}
7681

7782
/**

0 commit comments

Comments
 (0)