Skip to content

Commit e67e1df

Browse files
author
Denys Rul
committed
MAGETWO-57210: [github #6195, 4101] Gallery doesn't show all images added to configurable options
- Implement new behavior for a gallery on configurable product
1 parent 36b07c7 commit e67e1df

File tree

5 files changed

+37
-48
lines changed

5 files changed

+37
-48
lines changed

app/code/Magento/ConfigurableProduct/view/frontend/templates/product/view/type/options/configurable.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ $_attributes = $block->decorateArray($block->getAllowAttributes());
3535
"#product_addtocart_form": {
3636
"configurable": {
3737
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>,
38-
"onlyMainImg": <?php /* @escapeNotVerified */ echo $block->getVar('change_only_base_image', 'Magento_ConfigurableProduct') ?: 'false'; ?>
38+
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
39+
'Magento_ConfigurableProduct') ?: 'replace'; ?>"
3940
}
4041
}
4142
}

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ define([
2929
mediaGallerySelector: '[data-gallery-role=gallery-placeholder]',
3030
mediaGalleryInitial: null,
3131
slyOldPriceSelector: '.sly-old-price',
32-
onlyMainImg: false
32+
gallerySwitchStrategy: 'replace'
3333
},
3434

3535
/**
@@ -262,43 +262,30 @@ define([
262262
initialImages = $.extend(true, [], this.options.mediaGalleryInitial),
263263
galleryObject = $(this.options.mediaGallerySelector).data('gallery');
264264

265+
if (!galleryObject) {
266+
return;
267+
}
268+
265269
if (this.options.spConfig.images[this.simpleProduct]) {
266270
images = $.extend(true, [], this.options.spConfig.images[this.simpleProduct]);
267271
}
268272

269-
function updateGallery(imagesArr) {
270-
var imgToUpdate,
271-
mainImg;
272-
273-
mainImg = imagesArr.filter(function (img) {
274-
return img.isMain;
273+
if (images) {
274+
images.forEach(function (img) {
275+
img.type = 'image';
275276
});
276277

277-
imgToUpdate = mainImg.length ? mainImg[0] : imagesArr[0];
278-
galleryObject.updateDataByIndex(0, imgToUpdate);
279-
galleryObject.seek(1);
280-
}
281-
282-
if (galleryObject) {
283-
if (images) {
284-
images.map(function (img) {
285-
img.type = 'image';
286-
});
287-
288-
if (this.options.onlyMainImg) {
289-
updateGallery(images);
290-
} else {
291-
galleryObject.updateData(images)
292-
}
293-
} else {
294-
if (this.options.onlyMainImg) {
295-
updateGallery(initialImages);
296-
} else {
297-
galleryObject.updateData(this.options.mediaGalleryInitial);
298-
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
299-
}
278+
if (this.options.gallerySwitchStrategy === 'prepend') {
279+
images = images.concat(initialImages);
300280
}
281+
282+
galleryObject.updateData(images);
283+
} else {
284+
galleryObject.updateData(initialImages);
285+
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
301286
}
287+
288+
galleryObject.first();
302289
},
303290

304291
/**

app/code/Magento/Swatches/view/frontend/templates/product/view/renderer.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"jsonSwatchConfig": <?php /* @escapeNotVerified */
1717
echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
1818
"mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>",
19-
"onlyMainImg": <?php /* @escapeNotVerified */ echo $block->getVar('change_only_base_image',
20-
'Magento_Swatches') ?: 'false'; ?>
19+
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
20+
'Magento_Swatches') ?: 'replace'; ?>"
2121
}
2222
}
2323
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ define([
244244
mediaGalleryInitial: [{}],
245245

246246
//
247-
onlyMainImg: false,
247+
gallerySwitchStrategy: 'replace',
248248

249249
// whether swatches are rendered in product list or on product page
250250
inProductList: false
@@ -1016,26 +1016,27 @@ define([
10161016
*/
10171017
updateBaseImage: function (images, context, isProductViewExist) {
10181018
var justAnImage = images[0],
1019-
updateImg,
1020-
imagesToUpdate,
1019+
initialImages = this.options.mediaGalleryInitial,
10211020
gallery = context.find(this.options.mediaGallerySelector).data('gallery'),
1022-
item;
1021+
imagesToUpdate,
1022+
isInitial;
10231023

10241024
if (isProductViewExist) {
10251025
imagesToUpdate = images.length ? this._setImageType($.extend(true, [], images)) : [];
1026+
isInitial = _.isEqual(imagesToUpdate, initialImages);
10261027

1027-
if (this.options.onlyMainImg) {
1028-
updateImg = imagesToUpdate.filter(function (img) {
1029-
return img.isMain;
1030-
});
1031-
item = updateImg.length ? updateImg[0] : imagesToUpdate[0];
1032-
gallery.updateDataByIndex(0, item);
1028+
if (this.options.gallerySwitchStrategy === 'prepend' && !isInitial) {
1029+
imagesToUpdate = imagesToUpdate.concat(initialImages);
1030+
}
10331031

1034-
gallery.seek(1);
1035-
} else {
1036-
gallery.updateData(imagesToUpdate);
1032+
gallery.updateData(imagesToUpdate);
1033+
1034+
if (isInitial) {
10371035
$(this.options.mediaGallerySelector).AddFotoramaVideoEvents();
10381036
}
1037+
1038+
gallery.first();
1039+
10391040
} else if (justAnImage && justAnImage.img) {
10401041
context.find('.product-image-photo').attr('src', justAnImage.img);
10411042
}

app/design/frontend/Magento/luma/etc/view.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@
253253
</vars>
254254

255255
<vars module="Magento_ConfigurableProduct">
256-
<var name="change_only_base_image">true</var>
256+
<var name="gallery_switch_strategy">prepend</var>
257257
</vars>
258258
<vars module="Magento_Swatches">
259-
<var name="change_only_base_image">true</var>
259+
<var name="gallery_switch_strategy">prepend</var>
260260
</vars>
261261

262262
<vars module="Js_Bundle">

0 commit comments

Comments
 (0)