Problem/Motivation
When handling an $order_item for which the price hasn't been calculated yet, AddToCartEvent gives a fatal error:
The website encountered an unexpected error. Please try again later.
Error: Call to a member function subtract() on null in Drupal\google_tag\Plugin\GoogleTag\Event\Commerce\AddToCartEvent->getData() (line 44 of modules/contrib/google_tag/src/Plugin/GoogleTag/Event/Commerce/AddToCartEvent.php).
Related issue: RemoveFromCartEvent occasionally WSOD
Steps to reproduce
Execute something like \Drupal::service('commerce_cart.cart_manager')->addOrderItem($cart, $order_item); with:
$order_item = OrderItem::create([
'type' => 'default',
'purchased_entity' => $variation->id(),
'quantity' => 1,
]);
Comments
Comment #3
anybodyThis is the related code:
assert($unit_price !== NULL);safes us in dev, but not in prod, I guess?We maybe this needs more defensive checking also for production?
This is major for Drupal Commerce projects.
Comment #5
anybodyMaybe something like this? (See MR)
#3422723: Error: Call to a member function getCurrencyCode() on null (Drupal Commerce WSOD!) should also be applied (and merged before this one).
Comment #6
thomas.frobieterWorks perfectly fine.
Comment #7
anybodyStatic patch attached until this is merged.