From 6ba50a1c48d1b5fa3a5f98b751dd6ea7036f97ca Mon Sep 17 00:00:00 2001 From: Joel Male Date: Sat, 13 Jun 2026 14:18:55 +1000 Subject: [PATCH] fix: prevent save() on array after clear() with database driver --- src/CartSession.php | 6 ++---- tests/Unit/DatabaseCartTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/CartSession.php b/src/CartSession.php index 5d6049d..8803e50 100644 --- a/src/CartSession.php +++ b/src/CartSession.php @@ -159,10 +159,8 @@ public function clear() $deleted = $this->session->delete(); - $this->session = [ - $this->itemsKey => [], - $this->conditionsKey => [], - ]; + $this->session[$this->itemsKey] = []; + $this->session[$this->conditionsKey] = []; return $deleted; } diff --git a/tests/Unit/DatabaseCartTest.php b/tests/Unit/DatabaseCartTest.php index ac8f37a..924ff07 100644 --- a/tests/Unit/DatabaseCartTest.php +++ b/tests/Unit/DatabaseCartTest.php @@ -123,4 +123,23 @@ expect($this->cart->getContent()->count())->toEqual(0, 'Cart content should be 0'); expect(MockCartModel::all()->count())->toEqual(0, 'The carts database table should have 0 rows'); }); + + test('can clear cart conditions after clearing the cart', function () { + $condition = new CartCondition([ + 'name' => '10% Off', + 'type' => 'coupon', + 'target' => 'subtotal', + 'value' => '-10%', + ]); + + $this->cart->add(1, 'Sample Item', 100.99, 2, []); + $this->cart->condition($condition); + + $this->cart->clear(); + + // This was throwing "Call to a member function save() on array" + // because clear() replaced $this->session with a plain array + expect(fn () => $this->cart->clearCartConditions())->not->toThrow(Error::class); + expect($this->cart->getConditions())->toBeEmpty(); + }); });