You are here

protected function SelectStoreTrait::selectStore in Commerce Core 8.2

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.

3 calls to SelectStoreTrait::selectStore()
AddToCartForm::buildEntity in modules/cart/src/Form/AddToCartForm.php
Builds an updated entity object based upon the submitted form values.
AddToCartForm::submitForm in modules/cart/src/Form/AddToCartForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
PurchasedEntityAvailableConstraintValidator::validate in modules/order/src/Plugin/Validation/Constraint/PurchasedEntityAvailableConstraintValidator.php
Checks if the passed value is valid.

File

modules/store/src/SelectStoreTrait.php, line 35

Class

SelectStoreTrait
Provides a method for selecting the correct store for a purchasable entity.

Namespace

Drupal\commerce_store

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;
}