You are here

public function CartUnifier::combineCarts in Commerce Combine Carts 8

Combines another cart into the main cart and optionally deletes the other cart.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $main_cart: The main cart.

\Drupal\commerce_order\Entity\OrderInterface $other_cart: The other cart.

bool $delete: TRUE to delete the other cart when finished, FALSE to save it as empty.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to CartUnifier::combineCarts()
CartUnifier::assignCart in src/CartUnifier.php
Assign a cart to a user, possibly moving items to the user's main cart.
CartUnifier::combineUserCarts in src/CartUnifier.php
Combines all of a user's carts into their main cart.

File

src/CartUnifier.php, line 130

Class

CartUnifier

Namespace

Drupal\commerce_combine_carts

Code

public function combineCarts(OrderInterface $main_cart, OrderInterface $other_cart, $delete = FALSE) {
  if ($main_cart
    ->id() === $other_cart
    ->id()) {
    return;
  }
  if ($this
    ->isCartRequestedForCheckout($other_cart)) {
    return $this
      ->combineCarts($other_cart, $main_cart, $delete);
  }
  foreach ($other_cart
    ->getItems() as $item) {
    $other_cart
      ->removeItem($item);
    $item
      ->get('order_id')->entity = $main_cart;
    $combine = $this
      ->shouldCombineItem($item);
    $this->cartManager
      ->addOrderItem($main_cart, $item, $combine);
  }
  $main_cart
    ->save();
  if ($delete) {
    $other_cart
      ->delete();
  }
  else {
    $other_cart
      ->save();
  }
}