mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: add applies_to support for quantity-limited item conditions
This commit is contained in:
+26
-1
@@ -170,6 +170,31 @@ public function getPriceWithConditions($formatted = true)
|
||||
*/
|
||||
public function getPriceSumWithConditions($formatted = true)
|
||||
{
|
||||
return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config);
|
||||
$conditions = $this->hasConditions() ? $this->getConditions() : [];
|
||||
$conditions = is_array($conditions) ? $conditions : [$conditions];
|
||||
|
||||
$hasLimit = collect($conditions)->some(fn ($c) => $c->getAppliesToQuantity() !== null);
|
||||
|
||||
if (! $hasLimit) {
|
||||
return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config);
|
||||
}
|
||||
|
||||
$total = 0.0;
|
||||
|
||||
for ($i = 1; $i <= $this->quantity; $i++) {
|
||||
$price = $this->price;
|
||||
|
||||
foreach ($conditions as $condition) {
|
||||
$limit = $condition->getAppliesToQuantity();
|
||||
|
||||
if ($limit === null || $i <= $limit) {
|
||||
$price = $condition->applyCondition($price);
|
||||
}
|
||||
}
|
||||
|
||||
$total += $price;
|
||||
}
|
||||
|
||||
return Helpers::formatValue($total, $formatted, $this->config);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user