-
Notifications
You must be signed in to change notification settings - Fork 367
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 dynamically applying discounts #1243
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This doesn't seem correct.
The only issue I can see, which probably needs fixing, is the Cart discounts property is not being reset when calculating, which can lead to the same discount being logged twice. I believe it probably needs adding here https://github.com/lunarphp/lunar/blob/0.5/packages/core/src/Pipelines/Cart/ApplyDiscounts.php#L18 E.g. public function handle(Cart $cart, Closure $next)
{
$cart->discounts = collect([]); // <---------- this is new
$cart->discountBreakdown = collect([]); You probably need to update your test as follows // Calculate method called for the first time
CartSession::use($cart)->calculate(); |
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.
See comments
Hello @glennjacobs, I updated the test. And yes, there were 2 problems.
Let me know if I should do some more changes. Thank you. |
Sorry for spamming the comments, but I would love if if this could be merged for 0.6. The lack of a count()/empty() check on discounts is biting us at the moment so we have to manually add it. |
# Conflicts: # packages/core/tests/Unit/DiscountTypes/AmountOffTest.php
I came across a situation when discounts were not applied, because the
calculate
method was already called on cart. It is called duringCartSession::use($cart)
and also duringCartSession::current()
. This ended up with a state inDiscountManager
, when the$discounts
property was set to empty collection. Then the check in DiscountManager became true every time, because it only checks fornull
value and not empty collection.After changing the check to count with empty collection as follows:
! $this->discounts || $this->discounts?->isEmpty())
, there were multiple instances of the discounts added to$cart->discounts
property. I mitigated this behavior by only adding discounts which are not already present in that property.I also added a test which calls the
$cart->calculate()
method multiple times to make sure that discounts are added properly.All tests are running green now, but this implementation is ofc open for discussion. Maybe there is a better solution.