Skip to content

Commit e455bf0

Browse files
committed
fixed getTotal returning 0 when there is no conditions added
1 parent 918d44d commit e455bf0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ public function getTotal()
329329

330330
$conditions = $this->getConditions();
331331

332+
// if no conditions were added, just return the sub total
333+
if( ! $conditions->count() ) return $subTotal;
334+
332335
$conditions->each(function($cond) use ($subTotal, &$newTotal, &$process)
333336
{
334337
if( $cond->getTarget() === 'total' )

tests/CartConditionsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public function tearDown()
3737
m::close();
3838
}
3939

40+
public function test_total_without_condition()
41+
{
42+
$this->fillCart();
43+
44+
$this->assertEquals(187.49, $this->cart->getSubTotal(), 'Cart should have sub total of 187.49');
45+
46+
// no changes in subtotal as the condition's target added was for total
47+
$this->assertEquals(187.49, $this->cart->getSubTotal(), 'Cart should have sub total of 187.49');
48+
49+
// total should be the same as subtotal
50+
$this->assertEquals(187.49, $this->cart->getTotal(), 'Cart should have a total of 187.49');
51+
}
52+
4053
public function test_total_with_condition()
4154
{
4255
$this->fillCart();

0 commit comments

Comments
 (0)