mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +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);
|
||||
|
||||
Reference in New Issue
Block a user