New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix multiple cart rules calculation #16724
Conversation
c844763
to
fea810f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the issue #11722 still fixed with your modification? Is there a behat test dedicated to this previous issue?
src/Core/Cart/Calculator.php
Outdated
$initialShipping = $this->getFees()->getInitialShippingFees(); | ||
$finalShipping = $this->getFees()->getFinalShippingFees(); | ||
|
||
if (null !== $initialShipping && null !== $finalShipping) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I get this part Part of the shipping is added, the other one is subsided.. Which one is what? Maybe this justifies a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you compute the whole discount separately, is it still useful to applyFlatDiscount
on rows during the computation process?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I get this part Part of the shipping is added, the other one is subsided.. Which one is what? Maybe this justifies a comment
You're right, I'll add a comment explaining this part!
Since you compute the whole discount separately, is it still useful to
applyFlatDiscount
on rows during the computation process?
I think it's not useful but I'm pretty sure that getting rid of it will introduce a BC break?
Edit: Actually, it's needed because otherwise you can't keep track of an already applied discount. Imagine 2 discounts of 50% each with 2 different priorities. If we do not keep track of the already applied discount the cart total will be 0€ instead of 50% of 50% of the amount.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for applyFlatDiscount
For the shipping part I didn't get it at first but with your comment it seems logical, but I think we could simplify this an remove the comment with this:
if (null !== $initialShipping && null !== $finalShipping) {
$shippingDiscount = (new AmountImmutable())->add($initialShipping)->sub($finalShipping);
$totalWithoutDiscount = $totalWithoutDiscount->sub($shippingDiscount);
}
or to avoid the intermediate variables:
if (null !== $this->getFees()->getInitialShippingFees() && null !== $this->getFees()->getFinalShippingFees()) {
$shippingDiscount = (new AmountImmutable())
->add($this->getFees()->getInitialShippingFees())
->sub($this->getFees()->getFinalShippingFees())
;
$totalWithoutDiscount = $totalWithoutDiscount->sub($shippingDiscount);
}
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just updated the code 😉
Yes the issue is still fixed with my modifications. The behat tests I added handle that case too. |
8b26e87
to
2ab9486
Compare
src/Core/Cart/Calculator.php
Outdated
->add($this->getFees()->getInitialShippingFees()) | ||
->sub($this->getFees()->getFinalShippingFees()) | ||
; | ||
$totalWithoutDiscount = $totalWithoutDiscount->add($shippingDiscount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be a substraction here since it's a discount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe it should be added to $amount
? To have the addition of product discount plus shipping discount?
By the way does $totalWithoutDiscount
already contains shipping fees?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's the rest of the shipping discount so it should be added. Maybe I should rename the $shippingDiscount
to better reflect what this variable is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As seen with you on Slack, I changed the variable name to better understand that part
2ab9486
to
9917dc8
Compare
src/Core/Cart/Calculator.php
Outdated
@@ -150,10 +150,11 @@ public function getTotal($ignoreProcessedFlag = false) | |||
|
|||
$amount = new AmountImmutable(); | |||
foreach ($this->cartRows as $cartRow) { | |||
$rowPrice = $cartRow->getFinalTotalPrice(); | |||
$rowPrice = $cartRow->getInitialTotalPrice(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use getRowTotalWithoutDiscount
here instead of the loop
9917dc8
to
102e45a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job @atomiix
$shippingFees = $this->fees->getFinalShippingFees(); | ||
$amount = $this->getRowTotalWithoutDiscount(); | ||
$amount = $amount->sub($this->getDiscountTotal()); | ||
$shippingFees = $this->fees->getInitialShippingFees(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Thomas, could you please explain why using getInitialShippingFees()
here instead of getFinalShippingFees
? using getFinalShippingFees
makes avoiding #9586 (at least).
Hi, We have issues could be fixed with this PR: Thanks! |
Thanks @atomiix |
This change is