You are here

protected function CartAddResource::selectStore in Commerce Cart API 8

Selects the store for the given purchasable entity.

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 CartAddResource::selectStore()
CartAddResource::post in src/Plugin/rest/resource/CartAddResource.php
Add order items to the session's carts.

File

src/Plugin/rest/resource/CartAddResource.php, line 222

Class

CartAddResource
Creates order items for the session's carts.

Namespace

Drupal\commerce_cart_api\Plugin\rest\resource

Code

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

    // Malformed entity.
    throw new UnprocessableEntityHttpException('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 UnprocessableEntityHttpException("The given entity can't be purchased from the current store.");
    }
  }
  return $store;
}