Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetProductSalableQty.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
22 {
26  private $getStockItemConfiguration;
27 
31  private $getStockItemData;
32 
36  private $getReservationsQuantity;
37 
41  private $isSourceItemManagementAllowedForProductType;
42 
46  private $getProductTypesBySkus;
47 
55  public function __construct(
56  GetStockItemConfigurationInterface $getStockItemConfig,
57  GetStockItemDataInterface $getStockItemData,
58  GetReservationsQuantityInterface $getReservationsQuantity,
59  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
60  GetProductTypesBySkusInterface $getProductTypesBySkus
61  ) {
62  $this->getStockItemConfiguration = $getStockItemConfig;
63  $this->getStockItemData = $getStockItemData;
64  $this->getReservationsQuantity = $getReservationsQuantity;
65  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
66  $this->getProductTypesBySkus = $getProductTypesBySkus;
67  }
68 
72  public function execute(string $sku, int $stockId): float
73  {
74  $this->validateProductType($sku);
75  $stockItemData = $this->getStockItemData->execute($sku, $stockId);
76  if (null === $stockItemData || (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] === false) {
77  return 0;
78  }
79 
80  $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
81  $minQty = $stockItemConfig->getMinQty();
82 
83  $productQtyInStock = $stockItemData[GetStockItemDataInterface::QUANTITY]
84  + $this->getReservationsQuantity->execute($sku, $stockId)
85  - $minQty;
86 
87  return $productQtyInStock;
88  }
89 
94  private function validateProductType(string $sku): void
95  {
96  $productType = $this->getProductTypesBySkus->execute([$sku])[$sku];
97  if (false === $this->isSourceItemManagementAllowedForProductType->execute($productType)) {
98  throw new InputException(
99  __('Can\'t check requested quantity for products without Source Items support.')
100  );
101  }
102  }
103 }
__()
Definition: __.php:13
__construct(GetStockItemConfigurationInterface $getStockItemConfig, GetStockItemDataInterface $getStockItemData, GetReservationsQuantityInterface $getReservationsQuantity, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, GetProductTypesBySkusInterface $getProductTypesBySkus)