You are here

public function StockLocationStorage::getTransactionLocation in Commerce Stock 8

Get the transaction location for the given product and context.

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::getTransactionLocation

File

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

Class

StockLocationStorage
Defines the local stock location storage.

Namespace

Drupal\commerce_stock_local

Code

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

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

    // Get the available locations.
    $locations = $store->field_stock_allocation_location
      ->getValue();
    if (empty($locations)) {

      // Allocation field is empty.
      $locations = $this
        ->loadInContext($context, $entity);
      return empty($locations) ? NULL : array_shift($locations);
    }
    else {
      $location_id = array_shift($locations)['target_id'];
      $store_location = $this
        ->load($location_id);
      return $store_location;
    }
  }
  else {

    // No stock allocation field.
    $locations = $this
      ->loadInContext($context, $entity);
    return empty($locations) ? NULL : array_shift($locations);
  }
}