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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GoZ created an issue. See original summary.

GoZ’s picture

Status: Active » Needs review
FileSize
1.06 KB
GoZ’s picture

bojanz’s picture

Title: Error in recalculation in cas order item is referenced but nor more exists » Guard against deleted order items in Order::recalculateTotalPrice
Status: Needs review » Needs work

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

  • bojanz committed 76612ab on 8.x-2.x
    Issue #2987883 by GoZ, bojanz: Guard against deleted order items in...
bojanz’s picture

Status: Needs work » Fixed

Came 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!

Status: Fixed » Closed (fixed)

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