Commit 118b775a authored by Thomas Bilk's avatar Thomas Bilk

added new method to cart for getting a calculated list for price dependent conditions

parent 00d2fced
......@@ -534,7 +534,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
/**
* cart totel WITH cart conditions and WITH item conditions
* cart total WITH cart conditions and WITH item conditions
*
* @param null|string $type Only with this conditionType
* @return int
......@@ -629,6 +629,29 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
return (new Items($this->session->get($this->sessionKeyCartItems)));
}
/**
* get a collection of price conditions and their actual value
* @return Conditions
*/
public function priceConditions() {
$subtotal_with_conditions = $this->totalItemsWithoutConditions() + $this->totalOnlyConditions();
return $this->getConditions()
->filter(function (Condition $c) {
return $c->getTarget() === Condition::TARGET_PRICE;
})->map(function (Condition $c) use (&$subtotal_with_conditions) {
$cond_value = $c->applyCondition($subtotal_with_conditions);
$cond_data = $c->all();
if (($diff = $subtotal_with_conditions + $cond_value) < 0) {
$subtotal_with_conditions = 0;
$cond_data['value'] = (string)($cond_value - $diff);
} else {
$subtotal_with_conditions += $cond_value;
$cond_data['value'] = (string)$cond_value;
}
return new Condition($cond_data);
});
}
/**
* check if cart is empty
*
......
......@@ -1122,4 +1122,41 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(7500, $this->cart->subTotal());
$this->assertEquals(2125, $this->cart->total());
}
/**
* @throws \Bnet\Cart\Exceptions\InvalidConditionException
* @throws \Bnet\Cart\Exceptions\InvalidItemException
*/
public function test_cart_price_conditions() {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => 2500,
'quantity' => 3,
]);
$this->cart->condition([
new Condition([
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => '-5000',
]),
new Condition([
'name' => 'Coupon 2',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => '-3000',
]),
]);
$conditions = $this->cart->priceConditions();
$this->assertEquals(2, $conditions->count());
$this->assertEquals($this->cart->subTotal(), abs($conditions->sum('value')));
$this->cart->update(456, ['quantity' => '+1']);
$conditions = $this->cart->priceConditions();
$this->assertEquals(8000, abs($conditions->sum('value')));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment