Skip to content

Commit

Permalink
Cover situation when all of discount isnt actually able to be used (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Sep 29, 2023
1 parent a226892 commit 3b1c248
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
64 changes: 35 additions & 29 deletions packages/core/src/DiscountTypes/AmountOff.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,40 @@ private function applyFixedValue(array $values, Cart $cart): Cart
// enough left to apply the remaining too.
if ($remaining) {
// prioritise sharing the remaining over eligible lines
$line = $lines->first(function ($line) use ($remaining) {
return $line->subTotalDiscounted->value - $remaining > 0;
});

if ($line) {
$newDiscountTotal = $line->discountTotal->value + $remaining;

$line->discountTotal = new Price(
$newDiscountTotal,
$cart->currency,
1
);

$line->subTotalDiscounted = new Price(
$line->subTotal->value - $newDiscountTotal,
$cart->currency,
1
);

if (! $affectedLines->first(function ($breakdownLine) use ($line) {
return $breakdownLine->line == $line;
})) {
$affectedLines->push(new DiscountBreakdownLine(
line: $line,
quantity: $line->quantity
));
}
}
$lines->filter(function ($line) {
return $line->subTotalDiscounted->value > 0;
})
->each(function($line) use ($affectedLines, $cart, &$remaining) {
if ($remaining <= 0) {
return;
}

$amountAvailable = min($line->subTotalDiscounted->value, $remaining);
$remaining -= $amountAvailable;

$newDiscountTotal = $line->discountTotal->value + $amountAvailable;

$line->discountTotal = new Price(
$newDiscountTotal,
$cart->currency,
1
);

$line->subTotalDiscounted = new Price(
$line->subTotal->value - $newDiscountTotal,
$cart->currency,
1
);

if (! $affectedLines->first(function ($breakdownLine) use ($line) {
return $breakdownLine->line == $line;
})) {
$affectedLines->push(new DiscountBreakdownLine(
line: $line,
quantity: $line->quantity
));
}
});
}

if (! $cart->discounts) {
Expand All @@ -146,7 +152,7 @@ private function applyFixedValue(array $values, Cart $cart): Cart
$this->addDiscountBreakdown($cart, new DiscountBreakdown(
discount: $this->discount,
lines: $affectedLines,
price: new Price($value, $cart->currency, 1)
price: new Price($value - $remaining, $cart->currency, 1)
));

return $cart;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/Unit/DiscountTypes/AmountOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,9 +1356,9 @@ public function fixed_amount_discount_distributes_across_cart_lines_with_differe
$this->assertGreaterThanOrEqual(0, $fourthLine->subTotalDiscounted->value);
$this->assertGreaterThanOrEqual(0, $lastLine->subTotalDiscounted->value);

$this->assertEquals(149, $firstLine->discountTotal->value);
$this->assertEquals(198, $secondLine->discountTotal->value);
$this->assertEquals(399, $thirdLine->discountTotal->value);
$this->assertEquals(150, $firstLine->discountTotal->value);
$this->assertEquals(199, $secondLine->discountTotal->value);
$this->assertEquals(397, $thirdLine->discountTotal->value);
$this->assertEquals(397, $fourthLine->discountTotal->value);
$this->assertEquals(357, $lastLine->discountTotal->value);
$this->assertEquals(1500, $cart->discountTotal->value);
Expand Down

1 comment on commit 3b1c248

@vercel
Copy link

@vercel vercel bot commented on 3b1c248 Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.