Skip to content

Commit 4e5716f

Browse files
authored
Merge pull request #83 from aalyusuf/master
Additional needed methods
2 parents 2822e0f + b43ee3d commit 4e5716f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,22 @@ public function clearCartConditions()
508508
);
509509
}
510510

511+
/**
512+
* get cart sub total without conditions
513+
* @param bool $formatted
514+
* @return float
515+
*/
516+
public function getSubTotalWithoutConditions($formatted = true)
517+
{
518+
$cart = $this->getContent();
519+
520+
$sum = $cart->sum(function ($item) {
521+
return $item->getPriceSum();
522+
});
523+
524+
return Helpers::formatValue(floatval($sum), $formatted, $this->config);
525+
}
526+
511527
/**
512528
* get cart sub total
513529
* @param bool $formatted

src/Darryldecode/Cart/ItemCollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public function hasConditions()
6666
return false;
6767
}
6868

69+
/**
70+
* check if item has conditions
71+
*
72+
* @return mixed|null
73+
*/
74+
public function getConditions()
75+
{
76+
if(! $this->hasConditions() ) return [];
77+
return $this['conditions'];
78+
}
79+
6980
/**
7081
* get the single price in which conditions are already applied
7182
* @param bool $formatted

tests/ItemTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Darryldecode\Cart\Cart;
1010
use Mockery as m;
11+
use Darryldecode\Cart\CartCondition;
1112

1213
require_once __DIR__.'/helpers/SessionMock.php';
1314

@@ -55,4 +56,36 @@ public function test_item_get_sum_price_using_array_style()
5556

5657
$this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98');
5758
}
59+
60+
public function test_item_get_conditions_empty()
61+
{
62+
$this->cart->add(455, 'Sample Item', 100.99, 2, array());
63+
64+
$item = $this->cart->get(455);
65+
66+
$this->assertEmpty($item->getConditions(), 'Item should have no conditions');
67+
}
68+
69+
public function test_item_get_conditions_with_conditions()
70+
{
71+
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
72+
'name' => 'SALE 5%',
73+
'type' => 'sale',
74+
'target' => 'item',
75+
'value' => '-5%',
76+
));
77+
78+
$itemCondition2 = new CartCondition(array(
79+
'name' => 'Item Gift Pack 25.00',
80+
'type' => 'promo',
81+
'target' => 'item',
82+
'value' => '-25',
83+
));
84+
85+
$this->cart->add(455, 'Sample Item', 100.99, 2, array(),[$itemCondition1,$itemCondition2]);
86+
87+
$item = $this->cart->get(455);
88+
89+
$this->assertCount(2,$item->getConditions(), 'Item should have two conditions');
90+
}
5891
}

0 commit comments

Comments
 (0)