|
18 | 18 | use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
|
19 | 19 | use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
|
20 | 20 | use Magento\Catalog\Model\Product\Visibility;
|
| 21 | + |
21 | 22 | /**
|
22 | 23 | * Import entity product model
|
23 | 24 | * @SuppressWarnings(PHPMD.TooManyFields)
|
@@ -1190,7 +1191,7 @@ protected function _saveLinks()
|
1190 | 1191 | }
|
1191 | 1192 |
|
1192 | 1193 | $linkKey = "{$productId}-{$linkedId}-{$linkId}";
|
1193 |
| - if(empty($productLinkKeys[$linkKey])) { |
| 1194 | + if (empty($productLinkKeys[$linkKey])) { |
1194 | 1195 | $productLinkKeys[$linkKey] = $nextLinkId;
|
1195 | 1196 | }
|
1196 | 1197 | if (!isset($linkRows[$linkKey])) {
|
@@ -2087,65 +2088,15 @@ protected function _saveProductWebsites(array $websiteData)
|
2087 | 2088 | protected function _saveStockItem()
|
2088 | 2089 | {
|
2089 | 2090 | $indexer = $this->indexerRegistry->get('catalog_product_category');
|
| 2091 | + |
2090 | 2092 | /** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
|
2091 | 2093 | $stockResource = $this->_stockResItemFac->create();
|
2092 | 2094 | $entityTable = $stockResource->getMainTable();
|
2093 |
| - while ($bunch = $this->_dataSourceModel->getNextBunch()) { |
2094 |
| - $stockData = []; |
2095 |
| - $productIdsToReindex = []; |
2096 |
| - // Format bunch to stock data rows |
2097 |
| - foreach ($bunch as $rowNum => $rowData) { |
2098 |
| - if (!$this->isRowAllowedToImport($rowData, $rowNum)) { |
2099 |
| - continue; |
2100 |
| - } |
2101 | 2095 |
|
2102 |
| - $row = []; |
2103 |
| - $row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id']; |
2104 |
| - $productIdsToReindex[] = $row['product_id']; |
2105 |
| - |
2106 |
| - $row['website_id'] = $this->stockConfiguration->getDefaultScopeId(); |
2107 |
| - $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId(); |
2108 |
| - |
2109 |
| - $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']); |
2110 |
| - $existStockData = $stockItemDo->getData(); |
2111 |
| - |
2112 |
| - $row = array_merge( |
2113 |
| - $this->defaultStockData, |
2114 |
| - array_intersect_key($existStockData, $this->defaultStockData), |
2115 |
| - array_intersect_key($rowData, $this->defaultStockData), |
2116 |
| - $row |
2117 |
| - ); |
2118 |
| - |
2119 |
| - if ($this->stockConfiguration->isQty( |
2120 |
| - $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'] |
2121 |
| - )) { |
2122 |
| - $stockItemDo->setData($row); |
2123 |
| - $row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo); |
2124 |
| - if ($this->stockStateProvider->verifyNotification($stockItemDo)) { |
2125 |
| - $row['low_stock_date'] = $this->dateTime->gmDate( |
2126 |
| - 'Y-m-d H:i:s', |
2127 |
| - (new \DateTime())->getTimestamp() |
2128 |
| - ); |
2129 |
| - } |
2130 |
| - $row['stock_status_changed_auto'] = |
2131 |
| - (int) !$this->stockStateProvider->verifyStock($stockItemDo); |
2132 |
| - } else { |
2133 |
| - $row['qty'] = 0; |
2134 |
| - } |
2135 |
| - if (!isset($stockData[$rowData[self::COL_SKU]])) { |
2136 |
| - $stockData[$rowData[self::COL_SKU]] = $row; |
2137 |
| - } |
2138 |
| - } |
2139 |
| - |
2140 |
| - // Insert rows |
2141 |
| - if (!empty($stockData)) { |
2142 |
| - $this->_connection->insertOnDuplicate($entityTable, array_values($stockData)); |
2143 |
| - } |
2144 |
| - |
2145 |
| - if ($productIdsToReindex) { |
2146 |
| - $indexer->reindexList($productIdsToReindex); |
2147 |
| - } |
| 2096 | + while ($bunch = $this->_dataSourceModel->getNextBunch()) { |
| 2097 | + $this->formatBunchToStockDataRows($bunch, $entityTable, $indexer); |
2148 | 2098 | }
|
| 2099 | + |
2149 | 2100 | return $this;
|
2150 | 2101 | }
|
2151 | 2102 |
|
@@ -2739,4 +2690,74 @@ private function getProductIdentifierField()
|
2739 | 2690 | }
|
2740 | 2691 | return $this->productEntityIdentifierField;
|
2741 | 2692 | }
|
| 2693 | + |
| 2694 | + /** |
| 2695 | + * Get stock data rows from bunch. |
| 2696 | + * |
| 2697 | + * @param array $bunch |
| 2698 | + * @param string $entityTable |
| 2699 | + * @param \Magento\Framework\Indexer\IndexerInterface $indexer |
| 2700 | + * @return void |
| 2701 | + */ |
| 2702 | + private function formatBunchToStockDataRows( |
| 2703 | + $bunch, |
| 2704 | + $entityTable, |
| 2705 | + \Magento\Framework\Indexer\IndexerInterface $indexer |
| 2706 | + ) { |
| 2707 | + $stockData = []; |
| 2708 | + $productIdsToReindex = []; |
| 2709 | + |
| 2710 | + foreach ($bunch as $rowNum => $rowData) { |
| 2711 | + if (!$this->isRowAllowedToImport($rowData, $rowNum)) { |
| 2712 | + continue; |
| 2713 | + } |
| 2714 | + |
| 2715 | + $row = []; |
| 2716 | + $row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id']; |
| 2717 | + $row['website_id'] = $this->stockConfiguration->getDefaultScopeId(); |
| 2718 | + $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId(); |
| 2719 | + |
| 2720 | + $productIdsToReindex[] = $row['product_id']; |
| 2721 | + $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']); |
| 2722 | + $existStockData = $stockItemDo->getData(); |
| 2723 | + |
| 2724 | + $row = array_merge( |
| 2725 | + $this->defaultStockData, |
| 2726 | + array_intersect_key($existStockData, $this->defaultStockData), |
| 2727 | + array_intersect_key($rowData, $this->defaultStockData), |
| 2728 | + $row |
| 2729 | + ); |
| 2730 | + |
| 2731 | + if ($this->stockConfiguration->isQty( |
| 2732 | + $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'] |
| 2733 | + ) |
| 2734 | + ) { |
| 2735 | + $stockItemDo->setData($row); |
| 2736 | + $row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo); |
| 2737 | + if ($this->stockStateProvider->verifyNotification($stockItemDo)) { |
| 2738 | + $row['low_stock_date'] = $this->dateTime->gmDate( |
| 2739 | + 'Y-m-d H:i:s', |
| 2740 | + (new \DateTime())->getTimestamp() |
| 2741 | + ); |
| 2742 | + } |
| 2743 | + $row['stock_status_changed_auto'] = |
| 2744 | + (int)!$this->stockStateProvider->verifyStock($stockItemDo); |
| 2745 | + } else { |
| 2746 | + $row['qty'] = 0; |
| 2747 | + } |
| 2748 | + |
| 2749 | + if (!isset($stockData[$rowData[self::COL_SKU]])) { |
| 2750 | + $stockData[$rowData[self::COL_SKU]] = $row; |
| 2751 | + } |
| 2752 | + } |
| 2753 | + |
| 2754 | + // Insert rows |
| 2755 | + if (!empty($stockData)) { |
| 2756 | + $this->_connection->insertOnDuplicate($entityTable, array_values($stockData)); |
| 2757 | + } |
| 2758 | + |
| 2759 | + if ($productIdsToReindex && !$indexer->isScheduled()) { |
| 2760 | + $indexer->reindexList($productIdsToReindex); |
| 2761 | + } |
| 2762 | + } |
2742 | 2763 | }
|
0 commit comments