13
13
use Magento \CatalogImportExport \Model \Import \Product \ImageTypeProcessor ;
14
14
use Magento \CatalogImportExport \Model \Import \Product \MediaGalleryProcessor ;
15
15
use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface as ValidatorInterface ;
16
+ use Magento \CatalogImportExport \Model \Import \Product \StatusProcessor ;
17
+ use Magento \CatalogImportExport \Model \Import \Product \StockProcessor ;
16
18
use Magento \CatalogImportExport \Model \StockItemImporterInterface ;
17
19
use Magento \CatalogInventory \Api \Data \StockItemInterface ;
18
20
use Magento \Framework \App \Filesystem \DirectoryList ;
@@ -746,6 +748,15 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
746
748
*/
747
749
private $ productRepository ;
748
750
751
+ /**
752
+ * @var StatusProcessor
753
+ */
754
+ private $ statusProcessor ;
755
+ /**
756
+ * @var StockProcessor
757
+ */
758
+ private $ stockProcessor ;
759
+
749
760
/**
750
761
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
751
762
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -791,6 +802,8 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
791
802
* @param StockItemImporterInterface|null $stockItemImporter
792
803
* @param DateTimeFactory $dateTimeFactory
793
804
* @param ProductRepositoryInterface|null $productRepository
805
+ * @param StatusProcessor|null $statusProcessor
806
+ * @param StockProcessor|null $stockProcessor
794
807
* @throws LocalizedException
795
808
* @throws \Magento\Framework\Exception\FileSystemException
796
809
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -840,7 +853,9 @@ public function __construct(
840
853
MediaGalleryProcessor $ mediaProcessor = null ,
841
854
StockItemImporterInterface $ stockItemImporter = null ,
842
855
DateTimeFactory $ dateTimeFactory = null ,
843
- ProductRepositoryInterface $ productRepository = null
856
+ ProductRepositoryInterface $ productRepository = null ,
857
+ StatusProcessor $ statusProcessor = null ,
858
+ StockProcessor $ stockProcessor = null
844
859
) {
845
860
$ this ->_eventManager = $ eventManager ;
846
861
$ this ->stockRegistry = $ stockRegistry ;
@@ -876,6 +891,10 @@ public function __construct(
876
891
$ this ->mediaProcessor = $ mediaProcessor ?: ObjectManager::getInstance ()->get (MediaGalleryProcessor::class);
877
892
$ this ->stockItemImporter = $ stockItemImporter ?: ObjectManager::getInstance ()
878
893
->get (StockItemImporterInterface::class);
894
+ $ this ->statusProcessor = $ statusProcessor ?: ObjectManager::getInstance ()
895
+ ->get (StatusProcessor::class);
896
+ $ this ->stockProcessor = $ stockProcessor ?: ObjectManager::getInstance ()
897
+ ->get (StockProcessor::class);
879
898
parent ::__construct (
880
899
$ jsonHelper ,
881
900
$ importExportData ,
@@ -1290,12 +1309,18 @@ protected function _saveLinks()
1290
1309
protected function _saveProductAttributes (array $ attributesData )
1291
1310
{
1292
1311
$ linkField = $ this ->getProductEntityLinkField ();
1312
+ $ statusAttributeId = (int ) $ this ->retrieveAttributeByCode ('status ' )->getId ();
1293
1313
foreach ($ attributesData as $ tableName => $ skuData ) {
1314
+ $ linkIdBySkuForStatusChanged = [];
1294
1315
$ tableData = [];
1295
1316
foreach ($ skuData as $ sku => $ attributes ) {
1296
1317
$ linkId = $ this ->_oldSku [strtolower ($ sku )][$ linkField ];
1297
1318
foreach ($ attributes as $ attributeId => $ storeValues ) {
1298
1319
foreach ($ storeValues as $ storeId => $ storeValue ) {
1320
+ if ($ attributeId === $ statusAttributeId ) {
1321
+ $ this ->statusProcessor ->setStatus ($ sku , $ storeId , $ storeValue );
1322
+ $ linkIdBySkuForStatusChanged [strtolower ($ sku )] = $ linkId ;
1323
+ }
1299
1324
$ tableData [] = [
1300
1325
$ linkField => $ linkId ,
1301
1326
'attribute_id ' => $ attributeId ,
@@ -1305,6 +1330,9 @@ protected function _saveProductAttributes(array $attributesData)
1305
1330
}
1306
1331
}
1307
1332
}
1333
+ if ($ linkIdBySkuForStatusChanged ) {
1334
+ $ this ->statusProcessor ->loadOldStatus ($ linkIdBySkuForStatusChanged );
1335
+ }
1308
1336
$ this ->_connection ->insertOnDuplicate ($ tableName , $ tableData , ['value ' ]);
1309
1337
}
1310
1338
@@ -2188,6 +2216,7 @@ protected function _saveStockItem()
2188
2216
while ($ bunch = $ this ->_dataSourceModel ->getNextBunch ()) {
2189
2217
$ stockData = [];
2190
2218
$ productIdsToReindex = [];
2219
+ $ stockChangedProductIds = [];
2191
2220
// Format bunch to stock data rows
2192
2221
foreach ($ bunch as $ rowNum => $ rowData ) {
2193
2222
if (!$ this ->isRowAllowedToImport ($ rowData , $ rowNum )) {
@@ -2197,8 +2226,16 @@ protected function _saveStockItem()
2197
2226
$ row = [];
2198
2227
$ sku = $ rowData [self ::COL_SKU ];
2199
2228
if ($ this ->skuProcessor ->getNewSku ($ sku ) !== null ) {
2229
+ $ stockItem = $ this ->getRowExistingStockItem ($ rowData );
2230
+ $ existingStockItemData = $ stockItem ->getData ();
2200
2231
$ row = $ this ->formatStockDataForRow ($ rowData );
2201
2232
$ productIdsToReindex [] = $ row ['product_id ' ];
2233
+ $ storeId = $ this ->getRowStoreId ($ rowData );
2234
+ if (!empty (array_diff_assoc ($ row , $ existingStockItemData ))
2235
+ || $ this ->statusProcessor ->isStatusChanged ($ sku , $ storeId )
2236
+ ) {
2237
+ $ stockChangedProductIds [] = $ row ['product_id ' ];
2238
+ }
2202
2239
}
2203
2240
2204
2241
if (!isset ($ stockData [$ sku ])) {
@@ -2211,11 +2248,24 @@ protected function _saveStockItem()
2211
2248
$ this ->stockItemImporter ->import ($ stockData );
2212
2249
}
2213
2250
2251
+ $ this ->reindexStockStatus ($ stockChangedProductIds );
2214
2252
$ this ->reindexProducts ($ productIdsToReindex );
2215
2253
}
2216
2254
return $ this ;
2217
2255
}
2218
2256
2257
+ /**
2258
+ * Reindex stock status for provided product IDs
2259
+ *
2260
+ * @param array $productIds
2261
+ */
2262
+ private function reindexStockStatus (array $ productIds ): void
2263
+ {
2264
+ if ($ productIds ) {
2265
+ $ this ->stockProcessor ->reindexList ($ productIds );
2266
+ }
2267
+ }
2268
+
2219
2269
/**
2220
2270
* Initiate product reindex by product ids
2221
2271
*
@@ -3259,4 +3309,30 @@ private function composeLinkKey(int $productId, int $linkedId, int $linkTypeId)
3259
3309
{
3260
3310
return "{$ productId }- {$ linkedId }- {$ linkTypeId }" ;
3261
3311
}
3312
+
3313
+ /**
3314
+ * Get row store ID
3315
+ *
3316
+ * @param array $rowData
3317
+ * @return int
3318
+ */
3319
+ private function getRowStoreId (array $ rowData ): int
3320
+ {
3321
+ return !empty ($ rowData [self ::COL_STORE ])
3322
+ ? (int ) $ this ->getStoreIdByCode ($ rowData [self ::COL_STORE ])
3323
+ : Store::DEFAULT_STORE_ID ;
3324
+ }
3325
+
3326
+ /**
3327
+ * Get row stock item model
3328
+ *
3329
+ * @param array $rowData
3330
+ * @return StockItemInterface
3331
+ */
3332
+ private function getRowExistingStockItem (array $ rowData ): StockItemInterface
3333
+ {
3334
+ $ productId = $ this ->skuProcessor ->getNewSku ($ rowData [self ::COL_SKU ])['entity_id ' ];
3335
+ $ websiteId = $ this ->stockConfiguration ->getDefaultScopeId ();
3336
+ return $ this ->stockRegistry ->getStockItem ($ productId , $ websiteId );
3337
+ }
3262
3338
}
0 commit comments