9 use Magento\Catalog\Model\ProductFactory;
10 use Magento\CatalogInventory\Api\Data\StockItemCollectionInterfaceFactory;
12 use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
21 use Magento\Framework\DB\QueryBuilderFactory;
27 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
143 ->get(CollectionFactory::class);
153 $product = $this->productCollectionFactory->create()
154 ->setFlag(
'has_stock_status_filter')
156 ->addFieldToSelect(
'type_id')
162 $typeId =
$product->getTypeId() ?:
$product->getTypeInstance()->getTypeId();
163 $isQty = $this->stockConfiguration->isQty($typeId);
165 $isInStock = $this->stockStateProvider->verifyStock(
$stockItem);
166 if (
$stockItem->getManageStock() && !$isInStock) {
167 $stockItem->setIsInStock(
false)->setStockStatusChangedAutomaticallyFlag(
true);
171 if ($this->stockStateProvider->verifyNotification(
$stockItem)) {
172 $stockItem->setLowStockDate($this->dateTime->gmtDate());
175 if (
$stockItem->hasStockStatusChangedAutomaticallyFlag()) {
176 $stockItem->setStockStatusChangedAuto((
int)
$stockItem->getStockStatusChangedAutomaticallyFlag());
186 }
catch (\Exception $exception) {
187 throw new CouldNotSaveException(
__(
'The stock item was unable to be saved. Please try again.'), $exception);
195 public function get($stockItemId)
197 $stockItem = $this->stockItemFactory->create();
198 $this->resource->load(
$stockItem, $stockItemId);
201 __(
'The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.', $stockItemId)
210 public function getList(\
Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria)
212 $queryBuilder = $this->queryBuilderFactory->create();
213 $queryBuilder->setCriteria($criteria);
214 $queryBuilder->setResource($this->resource);
215 $query = $queryBuilder->create();
227 $this->getStockRegistryStorage()->removeStockItem(
$stockItem->getProductId());
228 $this->getStockRegistryStorage()->removeStockStatus(
$stockItem->getProductId());
232 'The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.',
251 __(
'The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.',
$id),
261 private function getStockRegistryStorage()
263 if (
null === $this->stockRegistryStorage) {
265 ->get(\
Magento\CatalogInventory\Model\StockRegistryStorage::class);
$productCollectionFactory
__construct(StockConfigurationInterface $stockConfiguration, StockStateProviderInterface $stockStateProvider, StockItemResource $resource, StockItemInterfaceFactory $stockItemFactory, StockItemCollectionInterfaceFactory $stockItemCollectionFactory, ProductFactory $productFactory, QueryBuilderFactory $queryBuilderFactory, MapperFactory $mapperFactory, TimezoneInterface $localeDate, Processor $indexProcessor, DateTime $dateTime, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory=null)
save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
getList(\Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria)
$stockItemCollectionFactory