Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In case (for some reasons), order item is deleted but order still has reference on it, recalculation will return an error:
Error: Call to a member function add() on null in Drupal\commerce_order\Entity\Order->recalculateTotalPrice() (line 373)
Proposed resolution
During adjustments loop, check total price is not null before adding amount.
If it's null, initiate value with the adjustment amount.
Comment | File | Size | Author |
---|---|---|---|
#2 | 2987883-2-order-recalculation-deleted-order-item.patch | 1.06 KB | GoZ |
|
Comments
Comment #2
GoZ CreditAttribution: GoZ at Iosan, Barbe-Rousse commentedComment #3
GoZ CreditAttribution: GoZ at Iosan, Barbe-Rousse commentedSee PR https://github.com/drupalcommerce/commerce/pull/857
Comment #4
bojanz CreditAttribution: bojanz at Centarro commentedBetter title.
Your patch fixes the crash, but it calculates a total even if no order items exist. That doesn't sound right. We should be keeping the total at NULL as long as there are no order items.
Comment #6
bojanz CreditAttribution: bojanz at Centarro commentedCame up with a failing test.
Stopped using ->hasItems() cause that check is performed internally anyway (in ->referencedEntities() which is wrapped by getItems()).
Added the right check for the total_price.
Thanks!