You are here

public function StockServiceManager::getStockLevel in Commerce Stock 8

Gets the total stock level for a given purchasable entity.

@todo - we should make the method more obscure as it does not support the context. Only useful for single store sites.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity to get the stock level for.

Return value

int The stock level.

Throws

\Exception

File

src/StockServiceManager.php, line 225

Class

StockServiceManager
The stock service manager.

Namespace

Drupal\commerce_stock

Code

public function getStockLevel(PurchasableEntityInterface $entity) {
  if ($entity
    ->isNew()) {
    return 0;
  }
  $stock_config = $this
    ->getService($entity)
    ->getConfiguration();
  $stock_checker = $this
    ->getService($entity)
    ->getStockChecker();
  $locations = $stock_config
    ->getAvailabilityLocations($this
    ->getContext($entity), $entity);
  return $stock_checker
    ->getTotalStockLevel($entity, $locations);
}