diff --git a/src/Cart.php b/src/Cart.php index dee5865..ae2d2f4 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -521,14 +521,10 @@ public function getSubTotalWithoutConditions($formatted = true) return Helpers::formatValue(floatval($sum), $formatted, $this->config); } - /** - * get cart sub total - * - * @param bool $formatted - */ public function getSubTotal($formatted = true): int|float|string { $subTotal = 0.00; + // add all the items together with conditions applied $subTotalSum = $this->getContent()->sum(function (ItemCollection $item) { return $item->getPriceSumWithConditions(false); diff --git a/src/CartSession.php b/src/CartSession.php index 06dbeb2..70de9ca 100644 --- a/src/CartSession.php +++ b/src/CartSession.php @@ -70,6 +70,32 @@ public function putItems($value) return $this->session->put($this->itemsKey, $value); } + // convert any conditions to arrays + $value = $value->toArray(); + + foreach ($value as $key => $item) { + if ($item['conditions']) { + if (is_array($item['conditions'])) { + $value[$key]['conditions'] = collect($item['conditions'])->map(function ($condition) { + return $condition->toArray(); + }); + } else { + $value[$key]['conditions'] = $item['conditions']->toArray(); + } + } + } + + // if ($value['conditions']) { + // $value['conditions'] = $value['conditions']->map(function ($condition) { + // return $condition->toArray(); + // }); + // } + + // dd($value); + // $value = $value->map(function ($item) { + // return $item->toArray(); + // }); + $this->session[$this->itemsKey] = $value; $this->session->save(); diff --git a/src/ItemCollection.php b/src/ItemCollection.php index 59866de..d34766f 100644 --- a/src/ItemCollection.php +++ b/src/ItemCollection.php @@ -91,7 +91,37 @@ public function getConditions() return []; } - return $this['conditions']; + $conditionsArray = []; + + $conditions = $this['conditions']; + + // check if we're already an array of CartConditions + foreach ($conditions as $key => $condition) { + if ($condition instanceof CartCondition) { + return $conditions; + } + } + + $hasSubArray = false; + + foreach ($conditions as $key => $condition) { + if (is_array($condition) && ! $condition instanceof CartCondition) { + $hasSubArray = true; + $conditionsArray[] = new CartCondition($condition); + } elseif ($condition instanceof CartCondition) { + $conditionsArray[] = $condition; + } + } + + if (! $hasSubArray) { + if ($conditions instanceof CartCondition) { + return $conditions; + } + + $conditionsArray[] = new CartCondition($conditions); + } + + return $conditionsArray; } /** @@ -108,14 +138,14 @@ public function getPriceWithConditions($formatted = true) $processed = 0; if ($this->hasConditions()) { - if (is_array($this->conditions)) { - foreach ($this->conditions as $condition) { + if (is_array($this->getConditions())) { + foreach ($this->getConditions() as $condition) { ($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice; $newPrice = $condition->applyCondition($toBeCalculated); $processed++; } } else { - $newPrice = $this['conditions']->applyCondition($originalPrice); + $newPrice = $this->getConditions()->applyCondition($originalPrice); } return Helpers::formatValue($newPrice, $formatted, $this->config); diff --git a/tests/Unit/DatabaseCartTest.php b/tests/Unit/DatabaseCartTest.php index 2e8a55f..f3deeda 100644 --- a/tests/Unit/DatabaseCartTest.php +++ b/tests/Unit/DatabaseCartTest.php @@ -1,6 +1,7 @@ cart->getContent()->count())->toEqual(0, 'Cart content should be 0'); expect($cartModel->items)->toEqual([], 'The carts database table should have 0 row'); }); + + test('can add an item with an item level condition', function () { + $saleCondition = new CartCondition([ + 'name' => '5% Discount', + 'type' => 'sale', + 'value' => '-5%', + ]); + + $item = [ + 'id' => 1, + 'name' => 'Chicken Curry', + 'price' => 18.95, + 'quantity' => 1, + 'attributes' => [], + 'conditions' => $saleCondition, + ]; + + $this->cart->add($item); + + expect($this->cart->get(1)->getPriceSum())->toEqual(18.95); + expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(18); + expect($this->cart->getSubTotal())->toEqual(18, 'Cart should have subtotal of 18'); + expect($this->cart->getTotal())->toEqual(18, 'Cart should have total of 18'); + }); + + test('can add an item with multiple item level conditions', function () { + $saleCondition = new CartCondition([ + 'name' => '5% Discount', + 'type' => 'sale', + 'value' => '-5%', + ]); + + $gstCondition = new CartCondition([ + 'name' => 'GST', + 'type' => 'gst', + 'value' => '2.5%', + ]); + + $item = [ + 'id' => 1, + 'name' => 'Chicken Curry', + 'price' => 18.95, + 'quantity' => 1, + 'attributes' => [], + 'conditions' => [$saleCondition, $gstCondition], + ]; + + $this->cart->add($item); + + expect($this->cart->get(1)->getPriceSum())->toEqual(18.95); + expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(18.45); + expect($this->cart->getSubTotal())->toEqual(18.45, 'Cart should have subtotal of 18.45'); + expect($this->cart->getTotal())->toEqual(18.45, 'Cart should have total of 18.45'); + }); });