You are here

protected function AddToCartController::selectStore in Commerce Add To Cart Link 8

Same name and namespace in other branches
  1. 2.x src/Controller/AddToCartController.php \Drupal\commerce_add_to_cart_link\Controller\AddToCartController::selectStore()

Selects the store for the given variation.

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

Parameters

\Drupal\commerce_product\Entity\ProductVariationInterface $variation: The variation being added to cart.

Return value

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

Throws

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

1 call to AddToCartController::selectStore()
AddToCartController::action in src/Controller/AddToCartController.php
Performs the add to cart action and redirects to cart.

File

src/Controller/AddToCartController.php, line 206

Class

AddToCartController
Defines the add to cart controller.

Namespace

Drupal\commerce_add_to_cart_link\Controller

Code

protected function selectStore(ProductVariationInterface $variation) {
  $stores = $variation
    ->getStores();
  if (count($stores) === 1) {
    $store = reset($stores);
  }
  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;
}