/** * @param string $productSku * @param null $websiteId * @return int * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getProductStockStatusBySku($productSku, $websiteId = null) { //if (!$websiteId) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); //} $productId = $this->resolveProductId($productSku); return $this->getProductStockStatus($productId, $websiteId); }
/** * @param int $productId * @param float $itemQty * @param float $qtyToCheck * @param float $origQty * @param int $websiteId * @return \Magento\Framework\Object */ public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $websiteId = null) { if ($websiteId === null) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); } $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId); return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty); }
/** * Retrieve Website Id * * @return int */ public function getWebsiteId() { $websiteId = $this->getData(static::WEBSITE_ID); if ($websiteId === null) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); } return (int) $websiteId; }
/** * Prepare stock item data for save * * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function saveStockItemData($product) { $stockItemData = $product->getStockData(); $stockItemData['product_id'] = $product->getId(); if (!isset($stockItemData['website_id'])) { $stockItemData['website_id'] = $this->stockConfiguration->getDefaultWebsiteId(); } $stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId(); foreach ($this->paramListToCheck as $dataKey => $configPath) { if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) { $stockItemData[$dataKey] = false; } } $originalQty = $product->getData('stock_data/original_inventory_qty'); if (strlen($originalQty) > 0) { $stockItemData['qty_correction'] = (isset($stockItemData['qty']) ? $stockItemData['qty'] : 0) - $originalQty; } // todo resolve issue with builder and identity field name $stockItem = $this->stockRegistry->getStockItem($stockItemData['product_id'], $stockItemData['website_id']); $stockItem->addData($stockItemData); $this->stockItemRepository->save($stockItem); return $this; }
/** * Get back to stock (when order is canceled or whatever else) * * @param int $productId * @param float $qty * @param int $websiteId * @return bool */ public function backItemQty($productId, $qty, $websiteId = null) { //if (!$websiteId) { $websiteId = $this->stockConfiguration->getDefaultWebsiteId(); //} $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId); if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) { if ($this->canSubtractQty($stockItem)) { $stockItem->setQty($stockItem->getQty() + $qty); } if ($this->stockConfiguration->getCanBackInStock($stockItem->getStoreId()) && $stockItem->getQty() > $stockItem->getMinQty()) { $stockItem->setIsInStock(true); $stockItem->setStockStatusChangedAutomaticallyFlag(true); } $stockItem->save(); } return true; }
/** * Stock item saving. * * @return $this */ protected function _saveStockItem() { $indexer = $this->indexerRegistry->get('catalog_product_category'); /** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */ $stockResource = $this->_stockResItemFac->create(); $entityTable = $stockResource->getMainTable(); while ($bunch = $this->_dataSourceModel->getNextBunch()) { $stockData = []; $productIdsToReindex = []; // Format bunch to stock data rows foreach ($bunch as $rowNum => $rowData) { if (!$this->isRowAllowedToImport($rowData, $rowNum)) { continue; } $row = []; $row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id']; $productIdsToReindex[] = $row['product_id']; $row['website_id'] = $this->stockConfiguration->getDefaultWebsiteId(); $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId(); $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']); $existStockData = $stockItemDo->getData(); $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row); if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'])) { $stockItemDo->setData($row); $row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo); if ($this->stockStateProvider->verifyNotification($stockItemDo)) { $row['low_stock_date'] = $this->_localeDate->date(null, null, false)->format('Y-m-d H:i:s'); } $row['stock_status_changed_auto'] = (int) (!$this->stockStateProvider->verifyStock($stockItemDo)); } else { $row['qty'] = 0; } $stockData[] = $row; } // Insert rows if (!empty($stockData)) { $this->_connection->insertOnDuplicate($entityTable, $stockData); } if ($productIdsToReindex) { $indexer->reindexList($productIdsToReindex); } } return $this; }