@@ -409,9 +409,12 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
409
409
/**
410
410
* Column names that holds images files names
411
411
*
412
+ * Note: the order of array items has a value in order to properly set 'position' value
413
+ * of media gallery items.
414
+ *
412
415
* @var string[]
413
416
*/
414
- protected $ _imagesArrayKeys = ['_media_image ' , ' image ' , 'small_image ' , 'thumbnail ' , 'swatch_image ' ];
417
+ protected $ _imagesArrayKeys = ['image ' , 'small_image ' , 'thumbnail ' , 'swatch_image ' , ' _media_image ' ];
415
418
416
419
/**
417
420
* Permanent entity columns.
@@ -1627,8 +1630,14 @@ protected function _saveProducts()
1627
1630
);
1628
1631
}
1629
1632
$ rowData [self ::COL_MEDIA_IMAGE ] = [];
1633
+
1634
+ /*
1635
+ * Note: to avoid problems with undefined sorting, the value of media gallery items positions
1636
+ * must be unique in scope of one product.
1637
+ */
1638
+ $ position = 0 ;
1630
1639
foreach ($ rowImages as $ column => $ columnImages ) {
1631
- foreach ($ columnImages as $ position => $ columnImage ) {
1640
+ foreach ($ columnImages as $ columnImageKey => $ columnImage ) {
1632
1641
if (!isset ($ uploadedImages [$ columnImage ])) {
1633
1642
$ uploadedFile = $ this ->uploadMediaFiles ($ columnImage , true );
1634
1643
if ($ uploadedFile ) {
@@ -1652,11 +1661,11 @@ protected function _saveProducts()
1652
1661
1653
1662
if ($ uploadedFile && !isset ($ mediaGallery [$ rowSku ][$ uploadedFile ])) {
1654
1663
if (isset ($ existingImages [$ rowSku ][$ uploadedFile ])) {
1655
- if (isset ($ rowLabels [$ column ][$ position ])
1656
- && $ rowLabels [$ column ][$ position ] != $ existingImages [$ rowSku ][$ uploadedFile ]['label ' ]
1664
+ if (isset ($ rowLabels [$ column ][$ columnImageKey ])
1665
+ && $ rowLabels [$ column ][$ columnImageKey ] != $ existingImages [$ rowSku ][$ uploadedFile ]['label ' ]
1657
1666
) {
1658
1667
$ labelsForUpdate [] = [
1659
- 'label ' => $ rowLabels [$ column ][$ position ],
1668
+ 'label ' => $ rowLabels [$ column ][$ columnImageKey ],
1660
1669
'imageData ' => $ existingImages [$ rowSku ][$ uploadedFile ]
1661
1670
];
1662
1671
}
@@ -1666,8 +1675,8 @@ protected function _saveProducts()
1666
1675
}
1667
1676
$ mediaGallery [$ rowSku ][$ uploadedFile ] = [
1668
1677
'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1669
- 'label ' => isset ($ rowLabels [$ column ][$ position ]) ? $ rowLabels [$ column ][$ position ] : '' ,
1670
- 'position ' => $ position + 1 ,
1678
+ 'label ' => isset ($ rowLabels [$ column ][$ columnImageKey ]) ? $ rowLabels [$ column ][$ columnImageKey ] : '' ,
1679
+ 'position ' => ++ $ position ,
1671
1680
'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1672
1681
'value ' => $ uploadedFile ,
1673
1682
];
0 commit comments