@@ -1221,39 +1221,65 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
1221
1221
return $ this ;
1222
1222
}
1223
1223
1224
+ /**
1225
+ * Retrieving images from all columns and rows
1226
+ *
1227
+ * @param $bunch
1228
+ * @return array
1229
+ */
1230
+ protected function _getAllBunchImages ($ bunch )
1231
+ {
1232
+ $ allImagesFromBunch = [];
1233
+ foreach ($ bunch as $ rowData ) {
1234
+ $ rowData = $ this ->_customFieldsMapping ($ rowData );
1235
+ foreach ($ this ->_imagesArrayKeys as $ image ) {
1236
+ $ dispersionPath =
1237
+ \Magento \Framework \File \Uploader::getDispretionPath ($ rowData [$ image ]);
1238
+ $ importImages = explode ($ this ->getMultipleValueSeparator (), $ rowData [$ image ]);
1239
+ foreach ($ importImages as $ importImage ) {
1240
+ $ imageSting = mb_strtolower (
1241
+ $ dispersionPath . '/ ' . preg_replace ('/[^a-z0-9\._-]+/i ' , '' , $ importImage )
1242
+ );
1243
+ $ allImagesFromBunch [$ importImage ] = $ imageSting ;
1244
+ }
1245
+ }
1246
+ }
1247
+ return $ allImagesFromBunch ;
1248
+ }
1249
+
1224
1250
/**
1225
1251
* Prepare all media files
1226
1252
*
1227
- * @return $this
1253
+ * @param $allImagesFromBunch
1254
+ * @return array
1228
1255
*/
1229
- protected function _prepareAllMediaFiles ()
1256
+ protected function _prepareAllMediaFiles ($ allImagesFromBunch )
1230
1257
{
1231
- static $ productEntityTableName = null ;
1232
1258
static $ productMediaGalleryTableName = null ;
1233
1259
static $ resource = null ;
1234
1260
if (!$ resource ) {
1235
1261
$ resource = $ this ->_resourceFactory ->create ();
1236
1262
}
1237
- if (!$ productEntityTableName ) {
1238
- $ productEntityTableName = $ resource ->getTable ('catalog_product_entity ' );
1239
- }
1240
1263
if (!$ productMediaGalleryTableName ) {
1241
1264
$ productMediaGalleryTableName = $ resource ->getTable ('catalog_product_entity_media_gallery ' );
1242
1265
}
1243
- if (empty ($ this ->cachedImages )) {
1244
- $ allMedia = $ this ->_connection ->fetchAll ($ this ->_connection ->select ()
1245
- ->from (
1246
- ["entity " => $ productEntityTableName ],
1247
- ['sku ' ]
1248
- )->joinLeft (
1249
- ["media_gallery " => $ productMediaGalleryTableName ],
1250
- "entity.entity_id = media_gallery.entity_id " ,
1251
- ['value ' ]
1252
- )
1253
- );
1254
- $ this ->cachedImages = $ allMedia ;
1266
+ $ allMedia = $ this ->_connection ->fetchAll ($ this ->_connection ->select ()
1267
+ ->from (
1268
+ $ productMediaGalleryTableName ,
1269
+ ['entity_id ' , 'value ' ]
1270
+ )->where ('value IN (?) ' , $ allImagesFromBunch )
1271
+ );
1272
+ $ result = array ();
1273
+ foreach ($ allMedia as $ image ) {
1274
+ $ result [$ image ['value ' ]] = [];
1275
+ foreach ($ this ->_oldSku as $ sku => $ oldSkuData ) {
1276
+ if ($ oldSkuData ['entity_id ' ] != $ image ['entity_id ' ]) {
1277
+ continue ;
1278
+ }
1279
+ $ result [$ image ['value ' ]][] = $ sku ;
1280
+ }
1255
1281
}
1256
- return $ this ;
1282
+ return $ result ;
1257
1283
}
1258
1284
1259
1285
/**
@@ -1269,7 +1295,6 @@ protected function _saveProducts()
1269
1295
/** @var $resource \Magento\CatalogImportExport\Model\Import\Proxy\Product\Resource */
1270
1296
$ resource = $ this ->_resourceFactory ->create ();
1271
1297
$ priceIsGlobal = $ this ->_catalogData ->isPriceGlobal ();
1272
- $ this ->_prepareAllMediaFiles ();
1273
1298
$ productLimit = null ;
1274
1299
$ productsQty = null ;
1275
1300
@@ -1285,6 +1310,8 @@ protected function _saveProducts()
1285
1310
$ uploadedGalleryFiles = [];
1286
1311
$ previousType = null ;
1287
1312
$ prevAttributeSet = null ;
1313
+ $ allImagesFromBunch = $ this ->_getAllBunchImages ($ bunch );
1314
+ $ existingImages = $ this ->_prepareAllMediaFiles ($ allImagesFromBunch );
1288
1315
1289
1316
foreach ($ bunch as $ rowNum => $ rowData ) {
1290
1317
if (!$ this ->validateRow ($ rowData , $ rowNum )) {
@@ -1334,9 +1361,9 @@ protected function _saveProducts()
1334
1361
$ this ->websitesCache [$ rowSku ] = [];
1335
1362
// 2. Product-to-Website phase
1336
1363
if (!empty ($ rowData [self ::COL_PRODUCT_WEBSITES ])) {
1337
- $ websiteIds = explode ($ this ->getMultipleValueSeparator (), $ rowData [self ::COL_PRODUCT_WEBSITES ]);
1338
- foreach ($ websiteIds as $ websiteId ) {
1339
- $ websiteId = $ this ->storeResolver ->getWebsiteCodeToId ($ rowData [ self :: COL_PRODUCT_WEBSITES ] );
1364
+ $ websiteCodes = explode ($ this ->getMultipleValueSeparator (), $ rowData [self ::COL_PRODUCT_WEBSITES ]);
1365
+ foreach ($ websiteCodes as $ websiteCode ) {
1366
+ $ websiteId = $ this ->storeResolver ->getWebsiteCodeToId ($ websiteCode );
1340
1367
$ this ->websitesCache [$ rowSku ][$ websiteId ] = true ;
1341
1368
}
1342
1369
}
@@ -1375,145 +1402,93 @@ protected function _saveProducts()
1375
1402
];
1376
1403
}
1377
1404
1378
- // 5. Media gallery phase
1405
+ if (!$ this ->validateRow ($ rowData , $ rowNum )) {
1406
+ continue ;
1407
+ }
1379
1408
1380
- $ fullDispersionPath = '' ;
1381
- $ imageIsSet = null ;
1382
- $ imageFromProduct = null ;
1383
- $ imageInProductIsSet = null ;
1409
+ // 5. Media gallery phase
1410
+ $ mediaGalleryImages = array ();
1411
+ $ mediaGalleryLabels = array ();
1384
1412
if (!empty ($ rowData [self ::COL_MEDIA_IMAGE ])) {
1385
- $ dispersionPath =
1386
- \Magento \Framework \File \Uploader::getDispretionPath ($ rowData [self ::COL_MEDIA_IMAGE ]);
1387
- $ imageName = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , $ rowData [self ::COL_MEDIA_IMAGE ]);
1388
- $ fullDispersionPath = mb_strtolower ($ dispersionPath . '/ ' . $ imageName );
1389
- foreach ($ this ->cachedImages as $ image ) {
1390
- if (($ image ['sku ' ] == $ rowData [self ::COL_SKU ])
1391
- && ($ image ['value ' ] == $ fullDispersionPath )
1392
- ) {
1393
- $ imageInProductIsSet = true ;
1394
- $ imageFromProduct = $ image ['value ' ];
1395
- break ;
1396
- } elseif (($ image ['sku ' ] == $ rowData [self ::COL_SKU ])
1397
- && (preg_replace ('/_[0-9]?\./ ' , '. ' , $ image ['value ' ]) == $ fullDispersionPath )
1398
- ) {
1399
- $ imageInProductIsSet = true ;
1400
- $ imageFromProduct = preg_replace ('/_[0-9]?\./ ' , '. ' , $ image ['value ' ]);
1401
- break ;
1402
- } elseif (in_array ($ fullDispersionPath , $ image )) {
1403
- $ imageIsSet = true ;
1404
- break ;
1405
- }
1413
+ $ mediaGalleryImages =
1414
+ explode ($ this ->getMultipleValueSeparator (), $ rowData [self ::COL_MEDIA_IMAGE ]);
1415
+ if (isset ($ rowData ['_media_image_label ' ])) {
1416
+ $ mediaGalleryLabels =
1417
+ explode ($ this ->getMultipleValueSeparator (), $ rowData ['_media_image_label ' ]);
1418
+ } else {
1419
+ $ mediaGalleryLabels = [];
1406
1420
}
1407
- }
1408
- if (($ imageInProductIsSet && ($ imageFromProduct != $ fullDispersionPath ))
1409
- || (!isset ($ imageIsSet ) && !isset ($ imageInProductIsSet ))
1410
- ) {
1411
- $ mediaGalleryImages = array ();
1412
- $ mediaGalleryLabels = array ();
1413
- if (!empty ($ rowData [self ::COL_MEDIA_IMAGE ])) {
1414
- $ mediaGalleryImages =
1415
- explode ($ this ->getMultipleValueSeparator (), $ rowData [self ::COL_MEDIA_IMAGE ]);
1416
- if (isset ($ rowData ['_media_image_label ' ])) {
1417
- $ mediaGalleryLabels =
1418
- explode ($ this ->getMultipleValueSeparator (), $ rowData ['_media_image_label ' ]);
1419
- } else {
1420
- $ mediaGalleryLabels = [];
1421
- }
1422
- if (count ($ mediaGalleryLabels ) > count ($ mediaGalleryImages )) {
1423
- $ mediaGalleryLabels = array_slice ($ mediaGalleryLabels , 0 , count ($ mediaGalleryImages ));
1424
- } elseif (count ($ mediaGalleryLabels ) < count ($ mediaGalleryImages )) {
1425
- $ mediaGalleryLabels = array_pad ($ mediaGalleryLabels , count ($ mediaGalleryImages ), '' );
1426
- }
1421
+ if (count ($ mediaGalleryLabels ) > count ($ mediaGalleryImages )) {
1422
+ $ mediaGalleryLabels = array_slice ($ mediaGalleryLabels , 0 , count ($ mediaGalleryImages ));
1423
+ } elseif (count ($ mediaGalleryLabels ) < count ($ mediaGalleryImages )) {
1424
+ $ mediaGalleryLabels = array_pad ($ mediaGalleryLabels , count ($ mediaGalleryImages ), '' );
1427
1425
}
1426
+ }
1428
1427
1429
- foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1430
- if (!empty ($ rowData [$ imageCol ])
1431
- && ($ imageCol != self ::COL_MEDIA_IMAGE )
1432
- && !in_array ($ rowData [$ imageCol ], $ mediaGalleryImages )) {
1433
- $ mediaGalleryImages [] = $ rowData [$ imageCol ];
1434
- if (isset ($ mediaGalleryLabels )) {
1435
- $ mediaGalleryLabels [] = isset ($ rowData [$ imageCol . '_label ' ])
1428
+ foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1429
+ if (!empty ($ rowData [$ imageCol ])
1430
+ && ($ imageCol != self ::COL_MEDIA_IMAGE )
1431
+ && !in_array ($ rowData [$ imageCol ], $ mediaGalleryImages )
1432
+ ) {
1433
+ $ mediaGalleryImages [] = $ rowData [$ imageCol ];
1434
+ if (isset ($ mediaGalleryLabels )) {
1435
+ $ mediaGalleryLabels [] = isset ($ rowData [$ imageCol . '_label ' ])
1436
1436
? $ rowData [$ imageCol . '_label ' ]
1437
1437
: '' ;
1438
- } else {
1439
- $ mediaGalleryLabels [] = '' ;
1440
- }
1438
+ } else {
1439
+ $ mediaGalleryLabels [] = '' ;
1441
1440
}
1442
1441
}
1443
-
1444
- $ rowData [self ::COL_MEDIA_IMAGE ] = array ();
1445
- foreach ($ mediaGalleryImages as $ mediaImage ) {
1442
+ }
1443
+ $ rowData [self ::COL_MEDIA_IMAGE ] = array ();
1444
+ foreach ($ mediaGalleryImages as $ mediaImage ) {
1445
+ $ imagePath = $ allImagesFromBunch [$ mediaImage ];
1446
+ if (isset ($ existingImages [$ imagePath ]) && in_array ($ rowSku , $ existingImages [$ imagePath ])) {
1446
1447
if (!array_key_exists ($ mediaImage , $ uploadedGalleryFiles )) {
1447
1448
$ uploadedGalleryFiles [$ mediaImage ] = $ this ->_uploadMediaFiles (
1448
- trim ($ mediaImage )
1449
+ trim ($ mediaImage ),
1450
+ true
1449
1451
);
1450
1452
}
1451
- $ rowData [self ::COL_MEDIA_IMAGE ][] = $ uploadedGalleryFiles [$ mediaImage ];
1452
- }
1453
-
1454
- foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1455
- if (!empty ($ rowData [$ imageCol ]) && ($ imageCol != self ::COL_MEDIA_IMAGE )) {
1456
- $ rowData [$ imageCol ] = $ uploadedGalleryFiles [$ rowData [$ imageCol ]];
1457
- }
1458
- }
1459
-
1460
- if (!empty ($ rowData [self ::COL_MEDIA_IMAGE ]) && is_array ($ rowData [self ::COL_MEDIA_IMAGE ])) {
1461
- $ position = 0 ;
1462
-
1463
- foreach ($ rowData [self ::COL_MEDIA_IMAGE ] as $ media_image ) {
1464
- $ mediaGallery [$ rowSku ][] = [
1465
- 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1466
- 'label ' => isset ($ mediaGalleryLabels [$ position ]) ? $ mediaGalleryLabels [$ position ] : '' ,
1467
- 'position ' => $ position ++,
1468
- 'disabled ' => '' ,
1469
- 'value ' => $ media_image ,
1470
- ];
1471
- }
1472
- }
1473
- } elseif ($ imageInProductIsSet && $ imageFromProduct == $ fullDispersionPath ) {
1474
- $ mediaGalleryImages = array ();
1475
- $ mediaGalleryLabels = array ();
1476
- if (!empty ($ rowData [self ::COL_MEDIA_IMAGE ])) {
1477
- $ mediaGalleryImages =
1478
- explode ($ this ->getMultipleValueSeparator (), $ rowData [self ::COL_MEDIA_IMAGE ]);
1479
- if (isset ($ rowData ['_media_image_label ' ])) {
1480
- $ mediaGalleryLabels =
1481
- explode ($ this ->getMultipleValueSeparator (), $ rowData ['_media_image_label ' ]);
1482
- } else {
1483
- $ mediaGalleryLabels = array ();
1484
- }
1485
- if (count ($ mediaGalleryLabels ) > count ($ mediaGalleryImages )) {
1486
- $ mediaGalleryLabels = array_slice ($ mediaGalleryLabels , 0 , count ($ mediaGalleryImages ));
1487
- } elseif (count ($ mediaGalleryLabels ) < count ($ mediaGalleryImages )) {
1488
- $ mediaGalleryLabels = array_pad ($ mediaGalleryLabels , count ($ mediaGalleryImages ), '' );
1489
- }
1490
- }
1491
-
1492
- foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1493
- if (!empty ($ rowData [$ imageCol ])
1494
- && ($ imageCol != self ::COL_MEDIA_IMAGE )
1495
- && !in_array ($ rowData [$ imageCol ], $ mediaGalleryImages )) {
1496
- $ mediaGalleryImages [] = $ rowData [$ imageCol ];
1497
- if (isset ($ rowData [$ imageCol . '_label ' ])) {
1498
- $ mediaGalleryLabels [] = $ rowData [$ imageCol . '_label ' ];
1499
- } else {
1500
- $ mediaGalleryLabels [] = '' ;
1501
- }
1502
- }
1503
- }
1504
-
1505
- $ rowData [self ::COL_MEDIA_IMAGE ] = array ();
1506
- foreach ($ mediaGalleryImages as $ mediaImage ) {
1453
+ } elseif (!isset ($ existingImages [$ imagePath ])) {
1507
1454
if (!array_key_exists ($ mediaImage , $ uploadedGalleryFiles )) {
1508
1455
$ uploadedGalleryFiles [$ mediaImage ] = $ this ->_uploadMediaFiles (
1509
1456
trim ($ mediaImage ),
1510
1457
true
1511
1458
);
1459
+ $ newImagePath = $ uploadedGalleryFiles [$ mediaImage ];
1460
+ $ existingImages [$ newImagePath ][] = $ rowSku ;
1512
1461
}
1513
1462
$ rowData [self ::COL_MEDIA_IMAGE ][] = $ uploadedGalleryFiles [$ mediaImage ];
1463
+ if (!empty ($ rowData [self ::COL_MEDIA_IMAGE ]) && is_array ($ rowData [self ::COL_MEDIA_IMAGE ])) {
1464
+ $ position = array_search ($ mediaImage , $ mediaGalleryImages );
1465
+ foreach ($ rowData [self ::COL_MEDIA_IMAGE ] as $ mediaImage ) {
1466
+ $ mediaGallery [$ rowSku ][] = [
1467
+ 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1468
+ 'label ' => isset ($ mediaGalleryLabels [$ position ]) ? $ mediaGalleryLabels [$ position ] : '' ,
1469
+ 'position ' => $ position ,
1470
+ 'disabled ' => '' ,
1471
+ 'value ' => $ mediaImage ,
1472
+ ];
1473
+ }
1474
+ }
1475
+ } else {
1476
+ foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1477
+ if (!empty ($ rowData [$ imageCol ]) && ($ rowData [$ imageCol ] == $ imagePath )) {
1478
+ $ rowData [$ imageCol ] = '' ;
1479
+ }
1480
+ }
1481
+ }
1482
+ if (!isset ($ existingImages [$ imagePath ]) || in_array ($ rowSku , $ existingImages [$ imagePath ])) {
1483
+ foreach ($ this ->_imagesArrayKeys as $ imageCol ) {
1484
+ if (!empty ($ rowData [$ imageCol ])
1485
+ && ($ imageCol != self ::COL_MEDIA_IMAGE )
1486
+ && isset ($ uploadedGalleryFiles [$ rowData [$ imageCol ]])
1487
+ ) {
1488
+ $ rowData [$ imageCol ] = $ uploadedGalleryFiles [$ rowData [$ imageCol ]];
1489
+ }
1490
+ }
1514
1491
}
1515
- } else {
1516
- $ this ->addRowError (__ ("Image already exists for '%s' " ), $ rowNum , self ::COL_MEDIA_IMAGE );
1517
1492
}
1518
1493
1519
1494
// 6. Attributes phase
0 commit comments