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).

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Status: Active » Needs review
StatusFileSize
new13.15 KB
new22.95 KB

The 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.

The last submitted patch, 2: 3125060-2-test-only.patch, failed testing. View results

jsacksick’s picture

StatusFileSize
new19.54 KB

Same 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.

jsacksick’s picture

Title: Promotion amounts should be calculated against the adjusted amounts » When stacking promotions, the order total can go past zero
mglaman’s picture

We 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.

+++ b/modules/order/src/Entity/OrderItem.php
@@ -183,6 +183,9 @@ class OrderItem extends CommerceContentEntityBase implements OrderItemInterface
+    if ($this->getOrder()) {
+      $this->getOrder()->recalculateTotalPrice();
+    }

@@ -191,6 +194,9 @@ class OrderItem extends CommerceContentEntityBase implements OrderItemInterface
+    if ($this->getOrder()) {
+      $this->getOrder()->recalculateTotalPrice();
+    }

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.

jsacksick’s picture

"The percentage offer needs to be based on the subtotal otherwise it's a diminishing discount." we're still doing that.

  • jsacksick committed eac8e6b on 8.x-2.x
    Issue #3125060 by jsacksick: Ensure stacking promotions doesn't result...
jsacksick’s picture

Status: Needs review » Fixed

Committed!

jsacksick’s picture

Status: Fixed » Needs review
StatusFileSize
new1.07 KB

Tests are now failing for some reason...

  • jsacksick committed 18a7445 on 8.x-2.x
    Issue #3125060 follow-up: Fix failing tests.
    
jsacksick’s picture

Status: Needs review » Fixed

Tests are now passing again, committing.

jacob.embree’s picture

Take a look at modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php:

-    if ($total_price->getCurrencyCode() != $amount->getCurrencyCode()) {
+    if ($adjusted_total_price->getCurrencyCode() != $adjusted_total_price->getCurrencyCode()) {

That can't be right.

jacob.embree’s picture

Status: Fixed » Needs work
jsacksick’s picture

Status: Needs work » Fixed

Thanks, fixed!

  • jsacksick committed 9d5317d on 8.x-2.x
    Issue #3125060 follow-up: Fix the currency code condition in...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.