Skip to content

Commit 016b010

Browse files
author
Sergey Semenov
committed
MAGETWO-64643: Product Image Import - Incorrect Position
1 parent 8b16cd5 commit 016b010

File tree

1 file changed

+16
-7
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+16
-7
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,12 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
409409
/**
410410
* Column names that holds images files names
411411
*
412+
* Note: the order of array items has a value in order to properly set 'position' value
413+
* of media gallery items.
414+
*
412415
* @var string[]
413416
*/
414-
protected $_imagesArrayKeys = ['_media_image', 'image', 'small_image', 'thumbnail', 'swatch_image'];
417+
protected $_imagesArrayKeys = ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
415418

416419
/**
417420
* Permanent entity columns.
@@ -1627,8 +1630,14 @@ protected function _saveProducts()
16271630
);
16281631
}
16291632
$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;
16301639
foreach ($rowImages as $column => $columnImages) {
1631-
foreach ($columnImages as $position => $columnImage) {
1640+
foreach ($columnImages as $columnImageKey => $columnImage) {
16321641
if (!isset($uploadedImages[$columnImage])) {
16331642
$uploadedFile = $this->uploadMediaFiles($columnImage, true);
16341643
if ($uploadedFile) {
@@ -1652,11 +1661,11 @@ protected function _saveProducts()
16521661

16531662
if ($uploadedFile && !isset($mediaGallery[$rowSku][$uploadedFile])) {
16541663
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']
16571666
) {
16581667
$labelsForUpdate[] = [
1659-
'label' => $rowLabels[$column][$position],
1668+
'label' => $rowLabels[$column][$columnImageKey],
16601669
'imageData' => $existingImages[$rowSku][$uploadedFile]
16611670
];
16621671
}
@@ -1666,8 +1675,8 @@ protected function _saveProducts()
16661675
}
16671676
$mediaGallery[$rowSku][$uploadedFile] = [
16681677
'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,
16711680
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
16721681
'value' => $uploadedFile,
16731682
];

0 commit comments

Comments
 (0)