/** * Prepare catalog inventory * * @param int[] $productIds * @return array */ protected function _prepareCatalogInventory(array $productIds) { if (empty($productIds)) { return array(); } $select = $this->_connection->select()->from($this->_itemFactory->create()->getMainTable())->where('product_id IN (?)', $productIds); $stmt = $this->_connection->query($select); $stockItemRows = array(); while ($stockItemRow = $stmt->fetch()) { $productId = $stockItemRow['product_id']; unset($stockItemRow['item_id'], $stockItemRow['product_id'], $stockItemRow['low_stock_date'], $stockItemRow['stock_id'], $stockItemRow['stock_status_changed_auto']); $stockItemRows[$productId] = $stockItemRow; } return $stockItemRows; }
/** * Stock item saving. * * @return $this */ protected function _saveStockItem() { /** @var $stockResource \Magento\CatalogInventory\Model\Resource\Stock\Item */ $stockResource = $this->_stockResItemFac->create(); $entityTable = $stockResource->getMainTable(); while ($bunch = $this->_dataSourceModel->getNextBunch()) { $stockData = array(); $productIdsToReindex = array(); // Format bunch to stock data rows foreach ($bunch as $rowNum => $rowData) { if (!$this->isRowAllowedToImport($rowData, $rowNum)) { continue; } // only SCOPE_DEFAULT can contain stock data if (self::SCOPE_DEFAULT != $this->getRowScope($rowData)) { continue; } $row = array(); $row['product_id'] = $this->_newSku[$rowData[self::COL_SKU]]['entity_id']; $productIdsToReindex[] = $row['product_id']; $row['stock_id'] = \Magento\CatalogInventory\Model\Stock\Item::DEFAULT_STOCK_ID; $stockItemDo = $this->stockItemService->getStockItem($row['product_id']); $existStockData = $stockItemDo->__toArray(); $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row); $row = $this->stockItemService->processIsInStock($row); if ($this->stockItemService->isQty($this->_newSku[$rowData[self::COL_SKU]]['type_id'])) { if ($this->stockItemService->verifyNotification($row['product_id'])) { $row['low_stock_date'] = $this->_localeDate->date(null, null, null, false)->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); } $row['stock_status_changed_auto'] = (int) (!$this->stockItemService->verifyStock($row['product_id'])); } else { $row['qty'] = 0; } $stockData[] = $row; } // Insert rows if (!empty($stockData)) { $this->_connection->insertOnDuplicate($entityTable, $stockData); } if ($productIdsToReindex) { $this->newIndexer->load('catalog_product_category')->reindexList($productIdsToReindex); } } return $this; }
/** * Stock item saving. * * @return $this */ protected function _saveStockItem() { $indexer = $this->indexerRegistry->get('catalog_product_category'); /** @var $stockResource \Magento\CatalogInventory\Model\Resource\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; }