diff --git a/src/Gloudemans/Shoppingcart/Cart.php b/src/Gloudemans/Shoppingcart/Cart.php index 20aa877..7c9ccae 100644 --- a/src/Gloudemans/Shoppingcart/Cart.php +++ b/src/Gloudemans/Shoppingcart/Cart.php @@ -211,7 +211,7 @@ class Cart { * @param Array $options Array of additional options, such as 'size' or 'color' * @return boolean */ - protected function generateRowId($id, $options) + public function generateRowId($id, $options) { return md5($id . serialize($options)); } @@ -344,4 +344,16 @@ class Cart { return $this->updateRow($rowId, $attributes); } + public function search($search) + { + foreach($this->getContent() as $item) + { + $rowId = $this->generateRowId($search['id'], $search['options']); + + if($rowId === $item->rowid) return true; + } + + return false; + } + } \ No newline at end of file diff --git a/tests/CartTest.php b/tests/CartTest.php index cfe6637..3d1ded7 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -189,4 +189,18 @@ class CartTest extends TestCase { $this->assertEquals($count, 2); } + public function testCartCanSearch() + { + Cart::add(1, 'test', 1, 10.00, ['size' => 'L']); + Cart::add(2, 'test', 2, 10.00, ['size' => 'L']); + + $search = Cart::search(['id' => 1, 'options' => ['size' => 'L']]); + + $this->assertTrue($search); + + $search = Cart::search(['id' => 3, 'options' => ['size' => 'L']]); + + $this->assertFalse($search); + } + } \ No newline at end of file