Skip to content

Commit 31c2ad4

Browse files
committed
Merge pull request #76 from AnastasiaBuniakEPAM/JS-323
JS-347: [SwatchRenderer.js] Instruct gallery to switch preview on new…
2 parents 08d4adf + 47cb1fa commit 31c2ad4

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,18 @@ define([
305305
}
306306

307307
function updateGallery(imagesArr) {
308-
var mainImg = imagesArr.filter(function (img) {
308+
var imgToUpdate,
309+
mainImg;
310+
311+
mainImg = imagesArr.filter(function (img) {
309312
return img.isMain;
310313
});
311-
!mainImg[0].type && (mainImg[0].type = 'image');
312314

313-
galleryObject.updateDataByIndex(0, mainImg[0]);
315+
imgToUpdate = mainImg.length ? mainImg[0] : imagesArr[0];
316+
317+
!imgToUpdate.type && (imgToUpdate.type = 'image');
318+
319+
galleryObject.updateDataByIndex(0, imgToUpdate);
314320
galleryObject.seek(1);
315321
}
316322

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ define([
866866
images.push({
867867
full: response.large,
868868
img: response.medium,
869-
thumb: response.small
869+
thumb: response.small,
870+
isMain: true
870871
});
871872

872873
if (response.hasOwnProperty('gallery')) {
@@ -912,22 +913,29 @@ define([
912913
*/
913914
updateBaseImage: function (images, context, isProductViewExist) {
914915
var justAnImage = images[0],
915-
imgs,
916-
imgToUpdate,
916+
updateImg,
917+
imagesToUpdate,
917918
gallery = context.find(this.options.mediaGallerySelector).data('gallery');
918919

919920
if (images) {
920-
imgs = $.extend(true, [], images);
921-
imgToUpdate = this._setImageType(imgs);
921+
imagesToUpdate = this._setImageType($.extend(true, [], images));
922922
}
923923

924924
if (isProductViewExist) {
925925
if (this.options.onlyMainImg) {
926-
//to be refactored - main img instead of 0;
927-
gallery.updateDataByIndex(0, imgToUpdate[0]);
926+
updateImg = imagesToUpdate.filter(function (img) {
927+
return img.isMain;
928+
});
929+
930+
if (updateImg.length) {
931+
gallery.updateDataByIndex(0, updateImg[0]);
932+
} else {
933+
gallery.updateDataByIndex(0, imagesToUpdate[0]);
934+
}
935+
928936
gallery.seek(1);
929937
} else {
930-
gallery.updateData(imgToUpdate);
938+
gallery.updateData(imagesToUpdate);
931939
}
932940
} else if (justAnImage && justAnImage.img) {
933941
context.find('.product-image-photo').attr('src', justAnImage.img);

0 commit comments

Comments
 (0)