You are here

public function StockLocationStorage::loadInContext in Commerce Stock 8

Loads the relevant locations for the given Purchasable Entity and context.

Relevant locations are active and available for fulfillment for the product and context provided.

Parameters

\Drupal\commerce\Context $context: The context.

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

Return value

\Drupal\commerce_stock_local\Entity\StockLocation[] The enabled stock locations.

Overrides StockLocationStorageInterface::loadInContext

1 call to StockLocationStorage::loadInContext()
StockLocationStorage::getTransactionLocation in modules/local_storage/src/StockLocationStorage.php
Get the transaction location for the given product and context.

File

modules/local_storage/src/StockLocationStorage.php, line 40

Class

StockLocationStorage
Defines the local stock location storage.

Namespace

Drupal\commerce_stock_local

Code

public function loadInContext(Context $context, PurchasableEntityInterface $entity) {
  $store = $context
    ->getStore();

  // Make sure we have the availability field for the location.
  if ($store
    ->hasField('field_available_stock_locations')) {

    // Get the available locations.
    $locations = $store->field_available_stock_locations
      ->getValue();

    // If no store locations.
    if (empty($locations)) {

      // Return the enabled locations.
      return $this
        ->loadEnabled($entity);
    }

    // Load them.
    $store_locations = [];
    foreach ($locations as $location) {
      $store_locations[$location['target_id']] = $location['target_id'];
    }
    $store_locations = $this
      ->loadMultiple($store_locations);

    // Remove if not enabled.
    foreach ($store_locations as $id => $location) {
      if (!$location
        ->isActive()) {
        unset($store_locations[$id]);
      }
    }

    // If no active store locations.
    if (empty($locations)) {

      // Return the enabled locations.
      return $this
        ->loadEnabled($entity);
    }
    return $store_locations;
  }
  else {

    // Return the enabled locations.
    return $this
      ->loadEnabled($entity);
  }
}