feat: add tests and fix item level conditions for database storage

This commit is contained in:
Joel Male
2025-02-25 16:31:42 +10:00
parent 158cf38f14
commit 7adf5ecd1f
4 changed files with 116 additions and 9 deletions
+1 -5
View File
@@ -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);
+26
View File
@@ -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
View File
@@ -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);