diff --git a/src/Drivers/SessionDriver.php b/src/Drivers/SessionDriver.php index 8af5687..0ec1bf9 100644 --- a/src/Drivers/SessionDriver.php +++ b/src/Drivers/SessionDriver.php @@ -37,8 +37,13 @@ public function setSessionKey(string $sessionKey): void $this->itemsKey = $sessionKey . '_cart_items'; $this->conditionsKey = $sessionKey . '_cart_conditions'; - $this->session->put($this->itemsKey, $items); - $this->session->put($this->conditionsKey, $conditions); + if ($items !== null) { + $this->session->put($this->itemsKey, $items); + } + + if ($conditions !== null) { + $this->session->put($this->conditionsKey, $conditions); + } } public function getDriverName(): string diff --git a/tests/Unit/Drivers/SessionDriverTest.php b/tests/Unit/Drivers/SessionDriverTest.php index c6c3f15..4cd2d63 100644 --- a/tests/Unit/Drivers/SessionDriverTest.php +++ b/tests/Unit/Drivers/SessionDriverTest.php @@ -70,4 +70,15 @@ test('getSessionModel returns null', function () { expect($this->driver->getSessionModel())->toBeNull(); }); + + test('setSessionKey on empty cart does not write null to session', function () { + $this->driver->setSessionKey('new-key'); + + // After key change, empty cart should still return empty arrays, not null + expect($this->driver->getItems())->toBe([]); + expect($this->driver->getConditions())->toBe([]); + // And the session should not have null values written + expect($this->session->get('new-key_cart_items'))->toBeNull(); + expect($this->session->get('new-key_cart_conditions'))->toBeNull(); + }); });