mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: add tests and fix item level conditions for database storage
This commit is contained in:
+1
-5
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
+34
-4
@@ -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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Joelwmale\Cart\Cart;
|
||||
use Joelwmale\Cart\CartCondition;
|
||||
use Joelwmale\Cart\Tests\Helpers\MockCartModel;
|
||||
|
||||
beforeEach(function () {
|
||||
@@ -52,4 +53,58 @@
|
||||
expect($this->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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user