You are here

public function ProductBundleStockProxy::getTotalStockLevel in Commerce Product Bundle 8

Gets the stock level.

Parameters

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

\Drupal\commerce_stock\StockLocationInterface[] $locations: The locations.

Return value

int The stock level.

Overrides StockCheckInterface::getTotalStockLevel

File

modules/commerce_product_bundle_stock/src/ProductBundleStockProxy.php, line 62

Class

ProductBundleStockProxy
Provides a stock service for product bundles.

Namespace

Drupal\commerce_product_bundle_stock

Code

public function getTotalStockLevel(PurchasableEntityInterface $bundle, array $locations) {
  $levels = array_map(function ($bundleItem) use ($bundle, $locations) {

    /** @var \Drupal\commerce_product_bundle\Entity\BundleItemInterface $bundleItem */
    $quantity = $bundleItem
      ->getQuantity() ?: 1;

    /** @var \Drupal\commerce\PurchasableEntityInterface $entity */
    $entity = $bundleItem
      ->getCurrentVariation();
    $service = $this->stockServiceManager
      ->getService($entity);
    $level = $service
      ->getStockChecker()
      ->getTotalStockLevel($entity, $locations);
    return floor($level / $quantity);
  }, $bundle
    ->getBundleItems());
  return min($levels);
}