Skip to content

Commit 57062f5

Browse files
author
Denys Rul
committed
MAGETWO-57210: [github #6195, 4101] Gallery doesn't show all images added to configurable options
- Remove redundant configuration for the Swatches module. - Add descrtiption of the "gallerySwitchStrategy" option.
1 parent e67e1df commit 57062f5

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ define([
2929
mediaGallerySelector: '[data-gallery-role=gallery-placeholder]',
3030
mediaGalleryInitial: null,
3131
slyOldPriceSelector: '.sly-old-price',
32+
33+
/**
34+
* Defines the mechanism of how images of a gallery should be
35+
* updated when user switches between configurations of a product.
36+
*
37+
* As for now value of this option can be either 'replace' or 'prepend'.
38+
*
39+
* @type {String}
40+
*/
3241
gallerySwitchStrategy: 'replace'
3342
},
3443

@@ -85,10 +94,10 @@ define([
8594

8695
this.inputSimpleProduct = this.element.find(options.selectSimpleProduct);
8796

88-
gallery.on('gallery:loaded', function () {
89-
var galleryObject = gallery.data('gallery');
90-
options.mediaGalleryInitial = galleryObject.returnCurrentImages();
91-
});
97+
gallery.data('gallery') ?
98+
this._onGalleryLoaded(gallery) :
99+
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));
100+
92101
},
93102

94103
/**
@@ -493,8 +502,18 @@ define([
493502
} else {
494503
$(this.options.slyOldPriceSelector).hide();
495504
}
496-
}
505+
},
497506

507+
/**
508+
* Callback which fired after gallery gets initialized.
509+
*
510+
* @param {HTMLElement} element - DOM element associated with gallery.
511+
*/
512+
_onGalleryLoaded: function (element) {
513+
var galleryObject = element.data('gallery');
514+
515+
this.options.mediaGalleryInitial = galleryObject.returnCurrentImages();
516+
}
498517
});
499518

500519
return $.mage.configurable;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
1818
"mediaCallback": "<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>",
1919
"gallerySwitchStrategy": "<?php /* @escapeNotVerified */ echo $block->getVar('gallery_switch_strategy',
20-
'Magento_Swatches') ?: 'replace'; ?>"
20+
'Magento_ConfigurableProduct') ?: 'replace'; ?>"
2121
}
2222
}
2323
}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,14 @@ define([
243243
// Cache for BaseProduct images. Needed when option unset
244244
mediaGalleryInitial: [{}],
245245

246-
//
246+
/**
247+
* Defines the mechanism of how images of a gallery should be
248+
* updated when user switches between configurations of a product.
249+
*
250+
* As for now value of this option can be either 'replace' or 'prepend'.
251+
*
252+
* @type {String}
253+
*/
247254
gallerySwitchStrategy: 'replace',
248255

249256
// whether swatches are rendered in product list or on product page
@@ -293,11 +300,9 @@ define([
293300
this.element.parents('.product-item-info');
294301

295302
if (isProductViewExist) {
296-
gallery.on('gallery:loaded', function () {
297-
var galleryObject = gallery.data('gallery');
298-
299-
options.mediaGalleryInitial = galleryObject.returnCurrentImages();
300-
});
303+
gallery.data('gallery') ?
304+
this._onGalleryLoaded(gallery) :
305+
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));
301306
} else {
302307
options.mediaGalleryInitial = [{
303308
'img': $main.find('.product-image-photo').attr('src')
@@ -1112,6 +1117,17 @@ define([
11121117
}
11131118

11141119
return selectedAttributes;
1120+
},
1121+
1122+
/**
1123+
* Callback which fired after gallery gets initialized.
1124+
*
1125+
* @param {HTMLElement} element - DOM element associated with a gallery.
1126+
*/
1127+
_onGalleryLoaded: function (element) {
1128+
var galleryObject = element.data('gallery');
1129+
1130+
this.options.mediaGalleryInitial = galleryObject.returnCurrentImages();
11151131
}
11161132
});
11171133

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@
255255
<vars module="Magento_ConfigurableProduct">
256256
<var name="gallery_switch_strategy">prepend</var>
257257
</vars>
258-
<vars module="Magento_Swatches">
259-
<var name="gallery_switch_strategy">prepend</var>
260-
</vars>
261258

262259
<vars module="Js_Bundle">
263260
<var name="bundle_size">1MB</var>

0 commit comments

Comments
 (0)