Skip to content

Commit 78e9fc0

Browse files
committed
MAGETWO-64856: [Backport] - Product Image Import - Incorrect Position - for 2.1
1 parent 54c2a0e commit 78e9fc0

File tree

5 files changed

+81
-14
lines changed

5 files changed

+81
-14
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,14 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
406406
];
407407

408408
/**
409-
* Column names that holds images files names
409+
* Column names that holds images files names.
410+
*
411+
* Note: the order of array items has a value in order to properly set 'position' value
412+
* of media gallery items.
410413
*
411414
* @var string[]
412415
*/
413-
protected $_imagesArrayKeys = ['_media_image', 'image', 'small_image', 'thumbnail', 'swatch_image'];
416+
protected $_imagesArrayKeys = ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
414417

415418
/**
416419
* Permanent entity columns.
@@ -1605,8 +1608,13 @@ protected function _saveProducts()
16051608
);
16061609
}
16071610
$rowData[self::COL_MEDIA_IMAGE] = [];
1611+
/*
1612+
* Note: to avoid problems with undefined sorting, the value of media gallery items positions
1613+
* must be unique in scope of one product.
1614+
*/
1615+
$position = 0;
16081616
foreach ($rowImages as $column => $columnImages) {
1609-
foreach ($columnImages as $position => $columnImage) {
1617+
foreach ($columnImages as $columnImageKey => $columnImage) {
16101618
if (!isset($uploadedImages[$columnImage])) {
16111619
$uploadedFile = $this->uploadMediaFiles(trim($columnImage), true);
16121620
if ($uploadedFile) {
@@ -1634,10 +1642,12 @@ protected function _saveProducts()
16341642
if ($column == self::COL_MEDIA_IMAGE) {
16351643
$rowData[$column][] = $uploadedFile;
16361644
}
1645+
$label = isset($rowLabels[$column][$columnImageKey]) ?
1646+
$rowLabels[$column][$columnImageKey] : '';
16371647
$mediaGallery[$rowSku][] = [
16381648
'attribute_id' => $this->getMediaGalleryAttributeId(),
1639-
'label' => isset($rowLabels[$column][$position]) ? $rowLabels[$column][$position] : '',
1640-
'position' => $position + 1,
1649+
'label' => $label,
1650+
'position' => ++$position,
16411651
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
16421652
'value' => $uploadedFile,
16431653
];

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ protected function getOptionValues(\Magento\Catalog\Model\Product\Option $option
561561
}
562562

563563
/**
564+
* Test that product import with images works properly
565+
*
564566
* @magentoDataIsolation enabled
565567
* @magentoDataFixture mediaImportImageFixture
566568
* @magentoAppIsolation enabled
@@ -593,7 +595,9 @@ public function testSaveMediaImage()
593595
->getInitParams()[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
594596
$uploader = $this->_model->getUploader();
595597

596-
$destDir = $directory->getRelativePath($appParams[DirectoryList::MEDIA][DirectoryList::PATH] . '/catalog/product');
598+
$destDir = $directory->getRelativePath(
599+
$appParams[DirectoryList::MEDIA][DirectoryList::PATH] . '/catalog/product'
600+
);
597601
$tmpDir = $directory->getRelativePath($appParams[DirectoryList::MEDIA][DirectoryList::PATH] . '/import');
598602

599603
$directory->create($destDir);
@@ -614,19 +618,46 @@ public function testSaveMediaImage()
614618
\Magento\Catalog\Model\Product::class
615619
);
616620
$product->load($productId);
621+
622+
$this->assertEquals('/m/a/magento_image.jpg', $product->getData('image'));
623+
$this->assertEquals('/m/a/magento_small_image.jpg', $product->getData('small_image'));
624+
$this->assertEquals('/m/a/magento_thumbnail.jpg', $product->getData('thumbnail'));
617625
$this->assertEquals('/m/a/magento_image.jpg', $product->getData('swatch_image'));
626+
618627
$gallery = $product->getMediaGalleryImages();
619628
$this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $gallery);
629+
620630
$items = $gallery->getItems();
621-
$this->assertCount(1, $items);
622-
$item = array_pop($items);
623-
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
624-
$this->assertEquals('/m/a/magento_image.jpg', $item->getFile());
625-
$this->assertEquals('Image Label', $item->getLabel());
631+
$this->assertCount(5, $items);
632+
633+
$imageItem = array_shift($items);
634+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $imageItem);
635+
$this->assertEquals('/m/a/magento_image.jpg', $imageItem->getFile());
636+
$this->assertEquals('Image Label', $imageItem->getLabel());
637+
638+
$smallImageItem = array_shift($items);
639+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $smallImageItem);
640+
$this->assertEquals('/m/a/magento_small_image.jpg', $smallImageItem->getFile());
641+
$this->assertEquals('Small Image Label', $smallImageItem->getLabel());
642+
643+
$thumbnailItem = array_shift($items);
644+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $thumbnailItem);
645+
$this->assertEquals('/m/a/magento_thumbnail.jpg', $thumbnailItem->getFile());
646+
$this->assertEquals('Thumbnail Label', $thumbnailItem->getLabel());
647+
648+
$additionalImageOneItem = array_shift($items);
649+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $additionalImageOneItem);
650+
$this->assertEquals('/m/a/magento_additional_image_one.jpg', $additionalImageOneItem->getFile());
651+
$this->assertEquals('Additional Image Label One', $additionalImageOneItem->getLabel());
652+
653+
$additionalImageTwoItem = array_shift($items);
654+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $additionalImageTwoItem);
655+
$this->assertEquals('/m/a/magento_additional_image_two.jpg', $additionalImageTwoItem->getFile());
656+
$this->assertEquals('Additional Image Label Two', $additionalImageTwoItem->getLabel());
626657
}
627658

628659
/**
629-
* Copy a fixture image into media import directory
660+
* Copy fixture images into media import directory.
630661
*/
631662
public static function mediaImportImageFixture()
632663
{
@@ -636,9 +667,35 @@ public static function mediaImportImageFixture()
636667
)->getDirectoryWrite(
637668
DirectoryList::MEDIA
638669
);
670+
639671
$mediaDirectory->create('import');
640672
$dirPath = $mediaDirectory->getAbsolutePath('import');
641-
copy(__DIR__ . '/../../../../Magento/Catalog/_files/magento_image.jpg', "{$dirPath}/magento_image.jpg");
673+
$items = [
674+
[
675+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_image.jpg',
676+
'dest' => $dirPath . '/magento_image.jpg',
677+
],
678+
[
679+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_small_image.jpg',
680+
'dest' => $dirPath . '/magento_small_image.jpg',
681+
],
682+
[
683+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_thumbnail.jpg',
684+
'dest' => $dirPath . '/magento_thumbnail.jpg',
685+
],
686+
[
687+
'source' => __DIR__ . '/_files/magento_additional_image_one.jpg',
688+
'dest' => $dirPath . '/magento_additional_image_one.jpg',
689+
],
690+
[
691+
'source' => __DIR__ . '/_files/magento_additional_image_two.jpg',
692+
'dest' => $dirPath . '/magento_additional_image_two.jpg',
693+
],
694+
];
695+
696+
foreach ($items as $item) {
697+
copy($item['source'], $item['dest']);
698+
}
642699
}
643700

644701
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label1,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,crosssell_skus,upsell_skus,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,associated_skus
2-
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,magento_image.jpg,,magento_image.jpg,,magento_image.jpg,,magento_image.jpg,,10/20/15 07:05,10/20/15 07:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",100,0,1,0,0,1,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,magento_image.jpg,Image Label,,,,,,,,
2+
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,magento_image.jpg,Image Label,magento_small_image.jpg,Small Image Label,magento_thumbnail.jpg,Thumbnail Label,magento_image.jpg,Image Label,10/20/15 07:05,10/20/15 07:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",100,0,1,0,0,1,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,"magento_additional_image_one.jpg, magento_additional_image_two.jpg","Additional Image Label One,Additional Image Label Two",,,,,,,,

0 commit comments

Comments
 (0)