Skip to content

Commit b26425d

Browse files
mglamanbojanz
authored andcommitted
Issue #2834628 by googletorp, mglaman: Fatal error when adding a order item to cart which doesn't have a purchaseable entity (#607)
1 parent 1d74e04 commit b26425d

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

modules/cart/src/CartManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ public function addOrderItem(OrderInterface $cart, OrderItemInterface $order_ite
115115
$cart->addItem($order_item);
116116
}
117117

118-
$event = new CartEntityAddEvent($cart, $purchased_entity, $quantity, $order_item);
119-
$this->eventDispatcher->dispatch(CartEvents::CART_ENTITY_ADD, $event);
118+
if ($purchased_entity) {
119+
$event = new CartEntityAddEvent($cart, $purchased_entity, $quantity, $order_item);
120+
$this->eventDispatcher->dispatch(CartEvents::CART_ENTITY_ADD, $event);
121+
}
122+
120123
if ($save_cart) {
121124
$cart->save();
122125
}

modules/cart/tests/src/Kernel/CartManagerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\Tests\commerce_cart\Kernel;
44

5+
use Drupal\commerce_order\Entity\OrderItem;
56
use Drupal\commerce_price\Price;
67
use Drupal\commerce_order\Entity\OrderInterface;
78
use Drupal\commerce_product\Entity\ProductVariation;
@@ -159,4 +160,21 @@ public function testCartManager() {
159160
$this->assertEquals(new Price('0.00', 'USD'), $cart->getTotalPrice());
160161
}
161162

163+
/**
164+
* Tests that order items without purchaseable entity do not cause crashes.
165+
*/
166+
public function testAddOrderItem() {
167+
$this->installCommerceCart();
168+
$cart = $this->cartProvider->createCart('default', $this->store, $this->user);
169+
170+
$order_item = OrderItem::create([
171+
'type' => 'default',
172+
'quantity' => 2,
173+
'unit_price' => new Price('12.00', 'USD'),
174+
]);
175+
$order_item->save();
176+
$this->cartManager->addOrderItem($cart, $order_item);
177+
$this->assertEquals(1, count($cart->getItems()));
178+
}
179+
162180
}

0 commit comments

Comments
 (0)