Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItemRepository.php
Go to the documentation of this file.
1 <?php
8 
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;
28 
33 {
38 
43 
47  protected $resource;
48 
52  protected $stockItemFactory;
53 
58 
62  protected $productFactory;
63 
68 
72  protected $mapperFactory;
73 
77  protected $localeDate;
78 
83  protected $indexProcessor;
84 
88  protected $dateTime;
89 
94 
99 
117  public function __construct(
121  StockItemInterfaceFactory $stockItemFactory,
122  StockItemCollectionInterfaceFactory $stockItemCollectionFactory,
123  ProductFactory $productFactory,
124  QueryBuilderFactory $queryBuilderFactory,
129  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null
130  ) {
131  $this->stockConfiguration = $stockConfiguration;
132  $this->stockStateProvider = $stockStateProvider;
133  $this->resource = $resource;
134  $this->stockItemFactory = $stockItemFactory;
135  $this->stockItemCollectionFactory = $stockItemCollectionFactory;
136  $this->productFactory = $productFactory;
137  $this->queryBuilderFactory = $queryBuilderFactory;
138  $this->mapperFactory = $mapperFactory;
139  $this->localeDate = $localeDate;
140  $this->indexProcessor = $indexProcessor;
141  $this->dateTime = $dateTime;
142  $this->productCollectionFactory = $productCollectionFactory ?: ObjectManager::getInstance()
143  ->get(CollectionFactory::class);
144  }
145 
149  public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
150  {
151  try {
153  $product = $this->productCollectionFactory->create()
154  ->setFlag('has_stock_status_filter')
155  ->addIdFilter($stockItem->getProductId())
156  ->addFieldToSelect('type_id')
157  ->getFirstItem();
158 
159  if (!$product->getId()) {
160  return $stockItem;
161  }
162  $typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
163  $isQty = $this->stockConfiguration->isQty($typeId);
164  if ($isQty) {
165  $isInStock = $this->stockStateProvider->verifyStock($stockItem);
166  if ($stockItem->getManageStock() && !$isInStock) {
167  $stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
168  }
169  // if qty is below notify qty, update the low stock date to today date otherwise set null
170  $stockItem->setLowStockDate(null);
171  if ($this->stockStateProvider->verifyNotification($stockItem)) {
172  $stockItem->setLowStockDate($this->dateTime->gmtDate());
173  }
174  $stockItem->setStockStatusChangedAuto(0);
175  if ($stockItem->hasStockStatusChangedAutomaticallyFlag()) {
176  $stockItem->setStockStatusChangedAuto((int)$stockItem->getStockStatusChangedAutomaticallyFlag());
177  }
178  } else {
179  $stockItem->setQty(0);
180  }
181 
182  $stockItem->setWebsiteId($stockItem->getWebsiteId());
183  $stockItem->setStockId($stockItem->getStockId());
184 
185  $this->resource->save($stockItem);
186  } catch (\Exception $exception) {
187  throw new CouldNotSaveException(__('The stock item was unable to be saved. Please try again.'), $exception);
188  }
189  return $stockItem;
190  }
191 
195  public function get($stockItemId)
196  {
197  $stockItem = $this->stockItemFactory->create();
198  $this->resource->load($stockItem, $stockItemId);
199  if (!$stockItem->getItemId()) {
200  throw new NoSuchEntityException(
201  __('The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.', $stockItemId)
202  );
203  }
204  return $stockItem;
205  }
206 
210  public function getList(\Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria)
211  {
212  $queryBuilder = $this->queryBuilderFactory->create();
213  $queryBuilder->setCriteria($criteria);
214  $queryBuilder->setResource($this->resource);
215  $query = $queryBuilder->create();
216  $collection = $this->stockItemCollectionFactory->create(['query' => $query]);
217  return $collection;
218  }
219 
223  public function delete(StockItemInterface $stockItem)
224  {
225  try {
226  $this->resource->delete($stockItem);
227  $this->getStockRegistryStorage()->removeStockItem($stockItem->getProductId());
228  $this->getStockRegistryStorage()->removeStockStatus($stockItem->getProductId());
229  } catch (\Exception $exception) {
230  throw new CouldNotDeleteException(
231  __(
232  'The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.',
233  $stockItem->getItemId()
234  ),
235  $exception
236  );
237  }
238  return true;
239  }
240 
244  public function deleteById($id)
245  {
246  try {
247  $stockItem = $this->get($id);
248  $this->delete($stockItem);
249  } catch (\Exception $exception) {
250  throw new CouldNotDeleteException(
251  __('The stock item with the "%1" ID wasn\'t found. Verify the ID and try again.', $id),
252  $exception
253  );
254  }
255  return true;
256  }
257 
261  private function getStockRegistryStorage()
262  {
263  if (null === $this->stockRegistryStorage) {
264  $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance()
265  ->get(\Magento\CatalogInventory\Model\StockRegistryStorage::class);
266  }
268  }
269 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__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)