The \Drupal\commerce_order\Entity\Order class methods does not check return value of \Drupal\commerce_order\Entity\OrderItemInterface::getTotalPrice() which may be NULL.

As a result, the following error may occur if order item is missing the price:

TypeError: Argument 1 passed to Drupal\commerce_price\Price::add() must be an instance of Drupal\commerce_price\Price, null given, called in /var/www/html/web/modules/contrib/commerce/modules/order/src/Entity/Order.php on line 361 in /var/www/html/web/modules/contrib/commerce/modules/price/src/Price.php on line 107 #0 /var/www/html/web/modules/contrib/commerce/modules/order/src/Entity/Order.php(361): Drupal\commerce_price\Price->add(NULL)

The code is:

$order_item_total = $order_item->getTotalPrice();
$total_price = $total_price ? $total_price->add($order_item_total) : $order_item_total;

This issue applies to \Drupal\commerce_order\Entity\Order::getSubtotalPrice() and \Drupal\commerce_order\Entity\Order::recalculateTotalPrice() methods.

In my case, the issue occurred during Migrate import; my order items was missing the price field which is expected behavior while working with stub items.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

abramm created an issue. See original summary.

abramm’s picture

Attaching the patch.

mglaman’s picture

In my case, the issue occurred during Migrate import; my order items was missing the price field which is expected behavior while working with stub items

The Commerce Migrate tests aren't failing on this. Or are those tests incomplete? Or are you not migrating from one of those expected sources?

abramm’s picture

Hi mglaman,

I'm migrating from 3rd party store.
Anyway, the issue is general purpose; the way I migrate just triggered it.

mglaman’s picture

Agreed we should fix the general error. Was curious if we could also do more for a stable data migration.

  • bojanz committed fb6167f on 8.x-2.x authored by abramm
    Issue #2954205 by abramm: Missing $order_item->getTotalPrice() return...
bojanz’s picture

Status: Needs review » Fixed

Tweaked and committed, thanks!

Status: Fixed » Closed (fixed)

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