test updates & working on calculating condition values based on orders

This commit is contained in:
Joel Male
2024-08-15 17:21:19 +10:00
parent 77fa8601b9
commit 85ae0c30c5
10 changed files with 1758 additions and 972 deletions
+52 -44
View File
@@ -360,7 +360,7 @@ public function clear()
*
* @throws InvalidConditionException
*/
public function condition($condition)
public function condition(CartCondition $condition)
{
if (is_array($condition)) {
foreach ($condition as $c) {
@@ -568,81 +568,89 @@ public function getSubTotalWithoutConditions($formatted = true)
* get cart sub total
*
* @param bool $formatted
* @return float
*/
public function getSubTotal($formatted = true)
public function getSubTotal($formatted = true): int|float
{
$cart = $this->getContent();
$sum = $cart->sum(function (ItemCollection $item) {
$subTotal = 0.00;
// add all the items together with conditions applied
$subTotalSum = $this->getContent()->sum(function (ItemCollection $item) {
return $item->getPriceSumWithConditions(false);
});
// get the conditions that are meant to be applied
// on the subtotal and apply it here before returning the subtotal
$conditions = $this
->getConditions()
->filter(function (CartCondition $cond) {
return $cond->getTarget() === 'subtotal';
$conditionsForSubtotal = $this->getConditions()
->filter(function (CartCondition $condition) {
return $condition->getTarget() === 'subtotal';
})
->sortBy(function (CartCondition $condition) {
return $condition->getOrder();
});
// if there is no conditions, lets just return the sum
if (! $conditions->count()) {
return Helpers::formatValue($sum, $formatted, $this->config);
if ($conditionsForSubtotal->isEmpty()) {
return Helpers::formatValue($subTotalSum, $formatted, $this->config);
}
// there are conditions, lets apply it
$newTotal = 0.00;
$process = 0;
$index = 0;
$conditions->each(function (CartCondition $cond) use ($sum, &$newTotal, &$process) {
$conditionsForSubtotal->each(function (CartCondition $condition) use ($subTotalSum, &$subTotal, &$index) {
$toBeCalculated = $index > 0 ? $subTotal : $subTotalSum;
// if this is the first iteration, the toBeCalculated
// should be the sum as initial point of value.
$toBeCalculated = ($process > 0) ? $newTotal : $sum;
$subTotal = $condition->applyCondition($toBeCalculated);
$newTotal = $cond->applyCondition($toBeCalculated);
$process++;
$index++;
});
return Helpers::formatValue((float) $newTotal, $formatted, $this->config);
return Helpers::formatValue($subTotal, $formatted, $this->config);
}
/**
* the new total in which conditions are already applied
*
* @return float
* Get the total of the cart with conditions applied
*/
public function getTotal()
public function getTotal(): int|float
{
$total = 0.00;
$subTotal = $this->getSubTotal(false);
$newTotal = 0.00;
$process = 0;
$conditions = $this
->getConditions()
->filter(function (CartCondition $cond) {
return $cond->getTarget() === 'total';
$conditionsForTotal = $this->getConditions()
->filter(function (CartCondition $condition) {
return $condition->getTarget() === 'total';
});
// if no conditions were added, just return the sub total
if (! $conditions->count()) {
if (! $conditionsForTotal->count()) {
return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config);
}
$conditions
->each(function (CartCondition $cond) use ($subTotal, &$newTotal, &$process) {
$toBeCalculated = ($process > 0) ? $newTotal : $subTotal;
$conditionsForTotal->each(function (CartCondition $condition) use ($subTotal, &$total) {
$toBeCalculated = $total > 0 ? $total : $subTotal;
$newTotal = $cond->applyCondition($toBeCalculated);
$total = $condition->applyCondition($toBeCalculated);
});
$process++;
});
return Helpers::formatValue($total, $this->config['format_numbers'], $this->config);
}
return Helpers::formatValue($newTotal, $this->config['format_numbers'], $this->config);
/**
* Get the calculated value for a condition by it's name
*/
public function getCalculatedValueForCondition(string $conditionName): int|float
{
$conditions = $this->getConditions();
$subTotal = $this->getSubTotalWithoutConditions(false);
foreach ($conditions as $condition) {
$conditionValue = $condition->getCalculatedValue($subTotal);
if ($condition->getName() === $conditionName) {
return $conditionValue;
}
$subTotal -= $conditionValue;
}
return 0;
}
/**
+20 -6
View File
@@ -16,7 +16,7 @@ class CartCondition
/**
* the parsed raw value of the condition
*/
public $parsedRawValue;
public $parsedRawValue = 0;
/**
* @param array $args (name, type, target, value)
@@ -77,14 +77,20 @@ public function getAttributes()
/**
* the value of this the condition
*
* @return mixed
*/
public function getValue()
public function getValue(): int|float
{
return $this->args['value'];
}
/**
* Set the value of this condition
*/
public function setValue($value): void
{
$this->args['value'] = $value;
}
/**
* Set the order to apply this condition. If no argument order is applied we return 0 as
* indicator that no assignment has been made
@@ -123,9 +129,11 @@ public function applyCondition($totalOrSubTotalOrPrice)
*
* @return mixed
*/
public function getCalculatedValue($totalOrSubTotalOrPrice)
public function getCalculatedValue($totalOrSubTotalOrPrice = null): int|float
{
$this->apply($totalOrSubTotalOrPrice, $this->getValue());
if ($totalOrSubTotalOrPrice) {
$this->apply($totalOrSubTotalOrPrice, $this->getValue());
}
return $this->parsedRawValue;
}
@@ -137,6 +145,12 @@ public function getCalculatedValue($totalOrSubTotalOrPrice)
*/
protected function apply($totalOrSubTotalOrPrice, $conditionValue)
{
if ($totalOrSubTotalOrPrice <= 0) {
$this->parsedRawValue = 0;
return;
}
// if value has a percentage sign on it, we will get first
// its percentage then we will evaluate again if the value
// has a minus or plus sign so we can decide what to do with the
+4
View File
@@ -56,6 +56,10 @@ public static function issetAndHasValueOrAssignDefault(&$var, $default = false)
public static function formatValue($value, $formatNumber, $config)
{
if (! $value) {
return 0;
}
if ($formatNumber || $config['format_numbers']) {
return floatval(number_format($value, $config['decimals'], $config['dec_point'], $config['thousands_sep']));
} else {