|
| 1 | +diff --git a/vendor/magento/module-inventory-import-export/Plugin/Import/SourceItemImporter.php b/vendor/magento/module-inventory-import-export/Plugin/Import/SourceItemImporter.php |
| 2 | +index d1c9a3e02c8..02bda87de4d 100644 |
| 3 | +--- a/vendor/magento/module-inventory-import-export/Plugin/Import/SourceItemImporter.php |
| 4 | ++++ b/vendor/magento/module-inventory-import-export/Plugin/Import/SourceItemImporter.php |
| 5 | +@@ -7,12 +7,24 @@ declare(strict_types=1); |
| 6 | + |
| 7 | + namespace Magento\InventoryImportExport\Plugin\Import; |
| 8 | + |
| 9 | +-use Magento\CatalogImportExport\Model\StockItemImporterInterface; |
| 10 | +-use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; |
| 11 | ++use Magento\CatalogImportExport\Model\Import\Product\SkuProcessor; |
| 12 | ++use Magento\CatalogImportExport\Model\StockItemProcessorInterface; |
| 13 | ++use Magento\Framework\App\ResourceConnection; |
| 14 | ++use Magento\Framework\Exception\CouldNotSaveException; |
| 15 | ++use Magento\Framework\Exception\InputException; |
| 16 | ++use Magento\Framework\Validation\ValidationException; |
| 17 | ++use Magento\Inventory\Model\ResourceModel\SourceItem; |
| 18 | + use Magento\InventoryApi\Api\Data\SourceItemInterface; |
| 19 | ++use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory; |
| 20 | + use Magento\InventoryApi\Api\SourceItemsSaveInterface; |
| 21 | + use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface; |
| 22 | ++use Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface; |
| 23 | + |
| 24 | ++/** |
| 25 | ++ * Assigning products to default source |
| 26 | ++ * |
| 27 | ++ * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | ++ */ |
| 29 | + class SourceItemImporter |
| 30 | + { |
| 31 | + /** |
| 32 | +@@ -36,57 +48,116 @@ class SourceItemImporter |
| 33 | + */ |
| 34 | + private $defaultSource; |
| 35 | + |
| 36 | ++ /** |
| 37 | ++ * @var IsSingleSourceModeInterface |
| 38 | ++ */ |
| 39 | ++ private $isSingleSourceMode; |
| 40 | ++ |
| 41 | ++ /** |
| 42 | ++ * @var ResourceConnection |
| 43 | ++ */ |
| 44 | ++ private $resourceConnection; |
| 45 | ++ |
| 46 | ++ /** |
| 47 | ++ * @var SkuProcessor |
| 48 | ++ */ |
| 49 | ++ private $skuProcessor; |
| 50 | ++ |
| 51 | + /** |
| 52 | + * StockItemImporter constructor |
| 53 | + * |
| 54 | + * @param SourceItemsSaveInterface $sourceItemsSave |
| 55 | + * @param SourceItemInterfaceFactory $sourceItemFactory |
| 56 | + * @param DefaultSourceProviderInterface $defaultSourceProvider |
| 57 | ++ * @param IsSingleSourceModeInterface $isSingleSourceMode |
| 58 | ++ * @param ResourceConnection $resourceConnection |
| 59 | ++ * @param SkuProcessor $skuProcessor |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + SourceItemsSaveInterface $sourceItemsSave, |
| 63 | + SourceItemInterfaceFactory $sourceItemFactory, |
| 64 | +- DefaultSourceProviderInterface $defaultSourceProvider |
| 65 | ++ DefaultSourceProviderInterface $defaultSourceProvider, |
| 66 | ++ IsSingleSourceModeInterface $isSingleSourceMode, |
| 67 | ++ ResourceConnection $resourceConnection, |
| 68 | ++ SkuProcessor $skuProcessor |
| 69 | + ) { |
| 70 | + $this->sourceItemsSave = $sourceItemsSave; |
| 71 | + $this->sourceItemFactory = $sourceItemFactory; |
| 72 | + $this->defaultSource = $defaultSourceProvider; |
| 73 | ++ $this->isSingleSourceMode = $isSingleSourceMode; |
| 74 | ++ $this->resourceConnection = $resourceConnection; |
| 75 | ++ $this->skuProcessor = $skuProcessor; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * After plugin Import to import Stock Data to Source Items |
| 80 | + * |
| 81 | +- * @param StockItemImporterInterface $subject |
| 82 | +- * @param null $result |
| 83 | ++ * @param StockItemProcessorInterface $subject |
| 84 | ++ * @param mixed $result |
| 85 | + * @param array $stockData |
| 86 | +- * @throws \Magento\Framework\Exception\CouldNotSaveException |
| 87 | +- * @throws \Magento\Framework\Exception\InputException |
| 88 | +- * @throws \Magento\Framework\Validation\ValidationException |
| 89 | ++ * @param array $importedData |
| 90 | + * @return void |
| 91 | +- * @see StockItemImporterInterface::import() |
| 92 | ++ * @throws CouldNotSaveException |
| 93 | ++ * @throws InputException |
| 94 | ++ * @throws ValidationException |
| 95 | + * |
| 96 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 97 | + */ |
| 98 | +- public function afterImport( |
| 99 | +- StockItemImporterInterface $subject, |
| 100 | ++ public function afterProcess( |
| 101 | ++ StockItemProcessorInterface $subject, |
| 102 | + $result, |
| 103 | +- array $stockData |
| 104 | +- ) { |
| 105 | ++ array $stockData, |
| 106 | ++ array $importedData |
| 107 | ++ ): void { |
| 108 | + $sourceItems = []; |
| 109 | ++ $skusHavingDefaultSource = $this->getSkusHavingDefaultSource(array_keys($stockData)); |
| 110 | ++ |
| 111 | + foreach ($stockData as $sku => $stockDatum) { |
| 112 | +- $inStock = (isset($stockDatum['is_in_stock'])) ? intval($stockDatum['is_in_stock']) : 0; |
| 113 | +- $qty = (isset($stockDatum['qty'])) ? $stockDatum['qty'] : 0; |
| 114 | +- /** @var SourceItemInterface $sourceItem */ |
| 115 | ++ $isNewSku = !array_key_exists(strtolower((string)$sku), $this->skuProcessor->getOldSkus()); |
| 116 | ++ $isQtyExplicitlySet = $importedData[$sku]['qty'] ?? false; |
| 117 | ++ |
| 118 | ++ $inStock = $stockDatum['is_in_stock'] ?? 0; |
| 119 | ++ $qty = $stockDatum['qty'] ?? 0; |
| 120 | + $sourceItem = $this->sourceItemFactory->create(); |
| 121 | + $sourceItem->setSku((string)$sku); |
| 122 | + $sourceItem->setSourceCode($this->defaultSource->getCode()); |
| 123 | + $sourceItem->setQuantity((float)$qty); |
| 124 | +- $sourceItem->setStatus($inStock); |
| 125 | +- $sourceItems[] = $sourceItem; |
| 126 | ++ $sourceItem->setStatus((int)$inStock); |
| 127 | ++ |
| 128 | ++ //Prevent existing products to be assigned to `default` source, when `qty` is not explicitly set. |
| 129 | ++ if ($isNewSku |
| 130 | ++ || $isQtyExplicitlySet |
| 131 | ++ || $this->isSingleSourceMode->execute() |
| 132 | ++ || in_array($sourceItem->getSku(), $skusHavingDefaultSource, true)) { |
| 133 | ++ $sourceItems[] = $sourceItem; |
| 134 | ++ } |
| 135 | + } |
| 136 | + if (count($sourceItems) > 0) { |
| 137 | + /** SourceItemInterface[] $sourceItems */ |
| 138 | + $this->sourceItemsSave->execute($sourceItems); |
| 139 | + } |
| 140 | + } |
| 141 | ++ |
| 142 | ++ /** |
| 143 | ++ * Fetch product's skus having assigned to `default` source. |
| 144 | ++ * |
| 145 | ++ * @param array $listSku |
| 146 | ++ * @return array |
| 147 | ++ */ |
| 148 | ++ private function getSkusHavingDefaultSource(array $listSku): array |
| 149 | ++ { |
| 150 | ++ $connection = $this->resourceConnection->getConnection(); |
| 151 | ++ $select = $connection->select()->from( |
| 152 | ++ $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM), |
| 153 | ++ [SourceItemInterface::SKU] |
| 154 | ++ )->where( |
| 155 | ++ SourceItemInterface::SKU . ' IN (?)', |
| 156 | ++ $listSku |
| 157 | ++ )->where( |
| 158 | ++ SourceItemInterface::SOURCE_CODE . ' = ?', |
| 159 | ++ $this->defaultSource->getCode() |
| 160 | ++ ); |
| 161 | ++ |
| 162 | ++ return $connection->fetchCol($select); |
| 163 | ++ } |
| 164 | + } |
| 165 | +diff --git a/vendor/magento/module-inventory-import-export/etc/di.xml b/vendor/magento/module-inventory-import-export/etc/di.xml |
| 166 | +index 5d0db315768..c6a09491c92 100644 |
| 167 | +--- a/vendor/magento/module-inventory-import-export/etc/di.xml |
| 168 | ++++ b/vendor/magento/module-inventory-import-export/etc/di.xml |
| 169 | +@@ -28,9 +28,11 @@ |
| 170 | + </type> |
| 171 | + <type name="Magento\CatalogImportExport\Model\StockItemImporterInterface"> |
| 172 | + <plugin name="updateConfigurableProductsStockItemStatusInventory" type="Magento\InventoryImportExport\Plugin\Import\UpdateConfigurableProductsPlugin" sortOrder="100" /> |
| 173 | +- <plugin name="importStockItemDataForSourceItem" type="Magento\InventoryImportExport\Plugin\Import\SourceItemImporter" /> |
| 174 | + <plugin name="update_configurable_products_stock_item_status" disabled="true"/> |
| 175 | + </type> |
| 176 | ++ <type name="Magento\CatalogImportExport\Model\StockItemProcessorInterface"> |
| 177 | ++ <plugin name="importStockItemDataForSourceItem" type="Magento\InventoryImportExport\Plugin\Import\SourceItemImporter"/> |
| 178 | ++ </type> |
| 179 | + <!-- Prevent stock indexer to execute twice as it is run by default --> |
| 180 | + <type name="Magento\CatalogImportExport\Model\Import\Product\StockProcessor"> |
| 181 | + <arguments> |
0 commit comments