public function CartManager::addOrderItem in Commerce Core 8.2
Adds the given order item to the given cart order.
Parameters
\Drupal\commerce_order\Entity\OrderInterface $cart: The cart order.
\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item.
bool $combine: Whether the order item should be combined with an existing matching one.
bool $save_cart: Whether the cart should be saved after the operation.
Return value
\Drupal\commerce_order\Entity\OrderItemInterface The saved order item.
Overrides CartManagerInterface::addOrderItem
1 call to CartManager::addOrderItem()
- CartManager::addEntity in modules/
cart/ src/ CartManager.php - Adds the given purchasable entity to the given cart order.
File
- modules/
cart/ src/ CartManager.php, line 103
Class
- CartManager
- Default implementation of the cart manager.
Namespace
Drupal\commerce_cartCode
public function addOrderItem(OrderInterface $cart, OrderItemInterface $order_item, $combine = TRUE, $save_cart = TRUE) {
$purchased_entity = $order_item
->getPurchasedEntity();
$quantity = $order_item
->getQuantity();
$matching_order_item = NULL;
if ($combine) {
$matching_order_item = $this->orderItemMatcher
->match($order_item, $cart
->getItems());
}
if ($matching_order_item) {
$new_quantity = Calculator::add($matching_order_item
->getQuantity(), $quantity);
$matching_order_item
->setQuantity($new_quantity);
$matching_order_item
->save();
$saved_order_item = $matching_order_item;
}
else {
$order_item
->set('order_id', $cart
->id());
$order_item
->save();
$cart
->addItem($order_item);
$saved_order_item = $order_item;
}
if ($purchased_entity) {
$event = new CartEntityAddEvent($cart, $purchased_entity, $quantity, $saved_order_item);
$this->eventDispatcher
->dispatch(CartEvents::CART_ENTITY_ADD, $event);
}
$event = new CartOrderItemAddEvent($cart, $quantity, $saved_order_item);
$this->eventDispatcher
->dispatch(CartEvents::CART_ORDER_ITEM_ADD, $event);
$this
->resetCheckoutStep($cart);
if ($save_cart) {
$cart
->save();
}
return $saved_order_item;
}