Skip to content

Commit 9469c40

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96593' into 2.2-develop-pr16
2 parents 6d3ecbf + 6f5b681 commit 9469c40

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
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: 36 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,67 @@ 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($product, $mediaEntry);
333336
}
334337

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

339-
$resultGallery = $this->getAllSizeImages($product, $baseImage);
342+
$resultGallery = $this->collectImageData($product, $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 ModelProduct $product
363+
* @param ProductAttributeMediaGalleryEntryInterface $mediaEntry
364+
* @return array
365+
*/
366+
private function collectImageData(
367+
ModelProduct $product,
368+
ProductAttributeMediaGalleryEntryInterface $mediaEntry
369+
): array {
370+
$image = $this->getAllSizeImages($product, $mediaEntry->getFile());
371+
$image[ProductAttributeMediaGalleryEntryInterface::POSITION] = $mediaEntry->getPosition();
372+
$image['isMain'] = $this->isMainImage($mediaEntry);
373+
return $image;
374+
}
375+
345376
/**
346377
* @param ModelProduct $product
347378
* @param string $imageFile

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,9 @@ define([
667667
/**
668668
* Load media gallery using ajax or json config.
669669
*
670-
* @param {String|undefined} eventName
671670
* @private
672671
*/
673-
_loadMedia: function (eventName) {
672+
_loadMedia: function () {
674673
var $main = this.inProductList ?
675674
this.element.parents('.product-item-info') :
676675
this.element.parents('.column.main'),
@@ -685,10 +684,21 @@ define([
685684
images = this.options.mediaGalleryInitial;
686685
}
687686

688-
this.updateBaseImage(images, $main, !this.inProductList, eventName);
687+
this.updateBaseImage(this._sortImages(images), $main, !this.inProductList);
689688
}
690689
},
691690

691+
/**
692+
* Sorting images array
693+
*
694+
* @private
695+
*/
696+
_sortImages: function (images) {
697+
return _.sortBy(images, function (image) {
698+
return image.position;
699+
});
700+
},
701+
692702
/**
693703
* Event for swatch options
694704
*
@@ -1252,9 +1262,6 @@ define([
12521262
dataMergeStrategy: this.options.gallerySwitchStrategy
12531263
});
12541264
}
1255-
1256-
gallery.first();
1257-
12581265
} else if (justAnImage && justAnImage.img) {
12591266
context.find('.product-image-photo').attr('src', justAnImage.img);
12601267
}

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)