Skip to content

Commit ff692d0

Browse files
MAGETWO-96593: Image Position issue with Associated Products
1 parent 6b109f4 commit ff692d0

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ define([
291291
images = this.options.spConfig.images[this.simpleProduct];
292292

293293
if (images) {
294+
images = this._sortImages(images);
295+
294296
if (this.options.gallerySwitchStrategy === 'prepend') {
295297
images = images.concat(initialImages);
296298
}
@@ -309,7 +311,17 @@ define([
309311
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
310312
}
311313

312-
galleryObject.first();
314+
},
315+
316+
/**
317+
* Sorting images array
318+
*
319+
* @private
320+
*/
321+
_sortImages: function (images) {
322+
return _.sortBy(images, function (image) {
323+
return image.position;
324+
});
313325
},
314326

315327
/**

app/code/Magento/Swatches/Controller/Ajax/Media.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
* Get product media for specified configurable product variation
4545
*
4646
* @return string
47+
* @throws \Magento\Framework\Exception\LocalizedException
4748
*/
4849
public function execute()
4950
{

app/code/Magento/Swatches/Helper/Data.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Swatches\Helper;
88

9+
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
910
use Magento\Catalog\Api\Data\ProductInterface as Product;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Helper\Image;
@@ -311,37 +312,64 @@ private function addFilterByParent(ProductCollection $productCollection, $parent
311312
* ]
312313
* @param ModelProduct $product
313314
* @return array
315+
* @throws \Magento\Framework\Exception\LocalizedException
314316
*/
315-
public function getProductMediaGallery(ModelProduct $product)
317+
public function getProductMediaGallery(ModelProduct $product): array
316318
{
317319
$baseImage = null;
318320
$gallery = [];
319321

320322
$mediaGallery = $product->getMediaGalleryEntries();
323+
/** @var ProductAttributeMediaGalleryEntryInterface $mediaEntry */
321324
foreach ($mediaGallery as $mediaEntry) {
322325
if ($mediaEntry->isDisabled()) {
323326
continue;
324327
}
325328

326-
if (in_array('image', $mediaEntry->getTypes(), true)) {
327-
$baseImage = $mediaEntry->getFile();
329+
if (!$baseImage || $this->isMainImage($mediaEntry)) {
330+
$baseImage = $mediaEntry;
328331
} elseif (!$baseImage) {
329332
$baseImage = $mediaEntry->getFile();
330333
}
331334

332-
$gallery[$mediaEntry->getId()] = $this->getAllSizeImages($product, $mediaEntry->getFile());
335+
$gallery[$mediaEntry->getId()] = $this->collectImageData($mediaEntry);
333336
}
334337

335338
if (!$baseImage) {
336339
return [];
337340
}
338341

339-
$resultGallery = $this->getAllSizeImages($product, $baseImage);
342+
$resultGallery = $this->collectImageData($baseImage);
340343
$resultGallery['gallery'] = $gallery;
341344

342345
return $resultGallery;
343346
}
344347

348+
/**
349+
* Checks if image is main image in gallery
350+
*
351+
* @param ProductAttributeMediaGalleryEntryInterface $mediaEntry
352+
* @return bool
353+
*/
354+
private function isMainImage(ProductAttributeMediaGalleryEntryInterface $mediaEntry): bool
355+
{
356+
return in_array('image', $mediaEntry->getTypes(), true);
357+
}
358+
359+
/**
360+
* Returns image data for swatches
361+
*
362+
* @param ProductAttributeMediaGalleryEntryInterface $mediaEntry
363+
* @return array
364+
*/
365+
private function collectImageData(ProductAttributeMediaGalleryEntryInterface $mediaEntry): array
366+
{
367+
$image = $this->getAllSizeImages($mediaEntry->getFile());
368+
$image[ProductAttributeMediaGalleryEntryInterface::POSITION] = $mediaEntry->getPosition();
369+
$image['isMain'] =$this->isMainImage($mediaEntry);
370+
return $image;
371+
}
372+
345373
/**
346374
* @param ModelProduct $product
347375
* @param string $imageFile

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,21 @@ define([
685685
images = this.options.mediaGalleryInitial;
686686
}
687687

688-
this.updateBaseImage(images, $main, !this.inProductList, eventName);
688+
this.updateBaseImage(this._sortImages(images), $main, !this.inProductList);
689689
}
690690
},
691691

692+
/**
693+
* Sorting images array
694+
*
695+
* @private
696+
*/
697+
_sortImages: function (images) {
698+
return _.sortBy(images, function (image) {
699+
return image.position;
700+
});
701+
},
702+
692703
/**
693704
* Event for swatch options
694705
*
@@ -1252,9 +1263,6 @@ define([
12521263
dataMergeStrategy: this.options.gallerySwitchStrategy
12531264
});
12541265
}
1255-
1256-
gallery.first();
1257-
12581266
} else if (justAnImage && justAnImage.img) {
12591267
context.find('.product-image-photo').attr('src', justAnImage.img);
12601268
}

lib/web/mage/gallery/gallery.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,18 @@ define([
472472
* @param {Array.<Object>} data - Set of gallery items to update.
473473
*/
474474
updateData: function (data) {
475+
var mainImageIndex;
476+
475477
if (_.isArray(data)) {
476478
settings.fotoramaApi.load(data);
479+
mainImageIndex = getMainImageIndex(data);
480+
481+
if (mainImageIndex) {
482+
settings.fotoramaApi.show({
483+
index: mainImageIndex,
484+
time: 0
485+
});
486+
}
477487

478488
$.extend(false, settings, {
479489
data: data,

0 commit comments

Comments
 (0)