Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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.
Comment | File | Size | Author |
---|---|---|---|
#2 | commerce-fix_missing_return_value_check-2954205-2-D8.patch | 1.27 KB | abramm |
|
Comments
Comment #2
abrammAttaching the patch.
Comment #3
mglamanThe Commerce Migrate tests aren't failing on this. Or are those tests incomplete? Or are you not migrating from one of those expected sources?
Comment #4
abrammHi mglaman,
I'm migrating from 3rd party store.
Anyway, the issue is general purpose; the way I migrate just triggered it.
Comment #5
mglamanAgreed we should fix the general error. Was curious if we could also do more for a stable data migration.
Comment #7
bojanz CreditAttribution: bojanz at Centarro commentedTweaked and committed, thanks!