From 11eea717a11471380f88cc09ee3ed8428826e600 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Tue, 7 Feb 2017 16:04:27 +0300 Subject: [PATCH 1/2] - Cart::getSubTotalWithoutConditions(); - $item->getConditions(); --- src/Darryldecode/Cart/Cart.php | 16 ++++++++++++++++ src/Darryldecode/Cart/ItemCollection.php | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Darryldecode/Cart/Cart.php b/src/Darryldecode/Cart/Cart.php index 48fffd0..baefc8c 100644 --- a/src/Darryldecode/Cart/Cart.php +++ b/src/Darryldecode/Cart/Cart.php @@ -508,6 +508,22 @@ public function clearCartConditions() ); } + /** + * get cart sub total without conditions + * @param bool $formatted + * @return float + */ + public function getSubTotalWithoutConditions($formatted = true) + { + $cart = $this->getContent(); + + $sum = $cart->sum(function ($item) { + return $item->getPriceSum(); + }); + + return Helpers::formatValue(floatval($sum), $formatted, $this->config); + } + /** * get cart sub total * @param bool $formatted diff --git a/src/Darryldecode/Cart/ItemCollection.php b/src/Darryldecode/Cart/ItemCollection.php index a1b2188..2b163ee 100644 --- a/src/Darryldecode/Cart/ItemCollection.php +++ b/src/Darryldecode/Cart/ItemCollection.php @@ -66,6 +66,17 @@ public function hasConditions() return false; } + /** + * check if item has conditions + * + * @return mixed|null + */ + public function getConditions() + { + if(! $this->hasConditions() ) return []; + return $this['conditions']; + } + /** * get the single price in which conditions are already applied * @param bool $formatted From b43ee3deaf2a78417abdc64c6328fd0cb68c9845 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Thu, 9 Mar 2017 01:17:17 +0300 Subject: [PATCH 2/2] tests for $item->getConditions(); --- tests/ItemTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 1d3b364..6ccd95b 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -8,6 +8,7 @@ use Darryldecode\Cart\Cart; use Mockery as m; +use Darryldecode\Cart\CartCondition; require_once __DIR__.'/helpers/SessionMock.php'; @@ -55,4 +56,36 @@ public function test_item_get_sum_price_using_array_style() $this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98'); } + + public function test_item_get_conditions_empty() + { + $this->cart->add(455, 'Sample Item', 100.99, 2, array()); + + $item = $this->cart->get(455); + + $this->assertEmpty($item->getConditions(), 'Item should have no conditions'); + } + + public function test_item_get_conditions_with_conditions() + { + $itemCondition1 = new \Darryldecode\Cart\CartCondition(array( + 'name' => 'SALE 5%', + 'type' => 'sale', + 'target' => 'item', + 'value' => '-5%', + )); + + $itemCondition2 = new CartCondition(array( + 'name' => 'Item Gift Pack 25.00', + 'type' => 'promo', + 'target' => 'item', + 'value' => '-25', + )); + + $this->cart->add(455, 'Sample Item', 100.99, 2, array(),[$itemCondition1,$itemCondition2]); + + $item = $this->cart->get(455); + + $this->assertCount(2,$item->getConditions(), 'Item should have two conditions'); + } } \ No newline at end of file