You are here

public function WishlistAssignment::assignMultiple in Commerce Wishlist 8.3

Assigns multiple anonymous wishlists to the given user account.

Parameters

\Drupal\commerce_wishlist\Entity\WishlistInterface[] $wishlists: The wishlists.

\Drupal\user\UserInterface $account: The account.

Overrides WishlistAssignmentInterface::assignMultiple

File

src/WishlistAssignment.php, line 91

Class

WishlistAssignment
Default wishlist assignment implementation.

Namespace

Drupal\commerce_wishlist

Code

public function assignMultiple(array $wishlists, UserInterface $account) {
  $allow_multiple = (bool) $this->configFactory
    ->get('commerce_wishlist.settings')
    ->get('allow_multiple');

  /** @var \Drupal\commerce_wishlist\WishlistStorageInterface $wishlist_storage */
  $wishlist_storage = $this->entityTypeManager
    ->getStorage('commerce_wishlist');
  foreach ($wishlists as $wishlist) {
    $default_wishlist = $wishlist_storage
      ->loadDefaultByUser($account, $wishlist
      ->bundle());

    // Check if multiple wishlists are allowed, in which case we're assigning
    // the wishlist to the given account.
    if ($allow_multiple || !$default_wishlist) {
      $this
        ->assign($wishlist, $account);
      continue;
    }

    // In case a single wishlist is allowed, we need to merge the wishlist
    // items with the default wishlist.
    $this->wishlistManager
      ->merge($wishlist, $default_wishlist);
  }
}