You are here

protected function WishlistUserForm::selectStore in Commerce Wishlist 8.3

Selects the store for the given purchasable entity.

Copied over from AddToCartForm.

If the entity is sold from one store, then that store is selected. If the entity is sold from multiple stores, and the current store is one of them, then that store is selected.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The entity being added to cart.

Return value

\Drupal\commerce_store\Entity\StoreInterface The selected store.

Throws

\Exception When the entity can't be purchased from the current store.

1 call to WishlistUserForm::selectStore()
WishlistUserForm::addItemToCart in src/Form/WishlistUserForm.php
Adds a wishlist item to the cart.

File

src/Form/WishlistUserForm.php, line 425

Class

WishlistUserForm
Provides the wishlist user form.

Namespace

Drupal\commerce_wishlist\Form

Code

protected function selectStore(PurchasableEntityInterface $entity) {
  $stores = $entity
    ->getStores();
  if (count($stores) === 1) {
    $store = reset($stores);
  }
  elseif (count($stores) === 0) {

    // Malformed entity.
    throw new \Exception('The given entity is not assigned to any store.');
  }
  else {
    $store = $this->currentStore
      ->getStore();
    if (!in_array($store, $stores)) {

      // Indicates that the site listings are not filtered properly.
      throw new \Exception("The given entity can't be purchased from the current store.");
    }
  }
  return $store;
}