Problem/Motivation
I discovered an interesting bug this week which occurs when multiple promotions are applied.
Basically, when multiple promotions are stacked (Most probably due to a contribution error), the order/order item totals can potentially be reduced past zero.
The reason for that is that we're applying the promotions against the order subtotal (for order amount fixed/percentage off offers) and against the order item total (for order item/percentage off offers).
As a consequence, if more than one promotion is applied, the second promotion is going to calculate its amount against the unreduced amount.
I've been thinking about the best way to fix this and I believe we should be calculating the promotions amounts against the adjusted totals (Patch to follow).
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3125060-10.patch | 1.07 KB | jsacksick |
| #4 | 3125060-4.patch | 19.54 KB | jsacksick |
Comments
Comment #2
jsacksick commentedThe attached patch is adding a new method to the order entity class
getAdjustedSubtotalPrice().I just have a doubt regarding order item offers, I do wonder if we should be calculating the amounts against the adjusted order item total there.
I'm also not adding the adjustment if the adjusted total is already 0 (in all offers).
I'm attaching a test-only patch that demonstrates the various failures, and the actual patch.
Comment #4
jsacksick commentedSame patch, without getAdjustedSubtotalPrice() which could be confusing.
As part of this, I think the order total has be recalculated when adding/removing an adjustment to order items (I'm wondering if this should be handled in a separate issue).
We're checking if the adjustment amount exceeds the subtotal today whereas I think we should ensure the adjustment doesn't exceed the order total (which include adjustments).
Also, I'm no longer adding the adjustment if the adjustment amount is 0.
Comment #5
jsacksick commentedComment #6
mglamanWe should ping in the #commerce channel. I don't see anything too controversial with using the total vs subtotal, because it's for Fixed promotions, so it's realistically calculated. The percentage offer needs to be based on the subtotal otherwise it's a diminishing discount.
This could be bad 😬. If the order field isn't populated with the existing reference... nothing bad would happen because we do not save.
So we'd have a problem if we saved here. But it could be useless if the order object isn't initiated yet.
Comment #7
jsacksick commented"The percentage offer needs to be based on the subtotal otherwise it's a diminishing discount." we're still doing that.
Comment #9
jsacksick commentedCommitted!
Comment #10
jsacksick commentedTests are now failing for some reason...
Comment #12
jsacksick commentedTests are now passing again, committing.
Comment #13
jacob.embree commentedTake a look at modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php:
That can't be right.
Comment #14
jacob.embree commentedComment #15
jsacksick commentedThanks, fixed!