Skip to content

Commit e911f08

Browse files
author
Yurii Hryhoriev
committed
MAGETWO-52060: Layered Navigation: Image placeholder shown on selected swatch instead of parent image
- swatches empty media fix - swatches media ajax calls caching is added - wishlist handling events fix
1 parent 9581647 commit e911f08

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

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

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ define([
184184
// Callback url for media
185185
mediaCallback: '',
186186

187+
// Local media cache
188+
mediaCache: {},
189+
187190
// Cache for BaseProduct images. Needed when option unset
188191
mediaGalleryInitial: [{}],
189192

@@ -708,7 +711,15 @@ define([
708711
$this = $widget.element,
709712
attributes = {},
710713
productId = 0,
711-
additional;
714+
mediaCallData,
715+
mediaCacheKey,
716+
mediaSuccessCallback = function (data) {
717+
if (!(mediaCacheKey in $widget.options.mediaCache)) {
718+
$widget.options.mediaCache[mediaCacheKey] = data;
719+
}
720+
$widget._ProductMediaCallback($this, data);
721+
$widget._DisableProductMediaLoader($this);
722+
};
712723

713724
if (!$widget.options.mediaCallback) {
714725
return;
@@ -729,24 +740,28 @@ define([
729740
.find('.price-box.price-final_price').attr('data-product-id');
730741
}
731742

732-
additional = $.parseQuery();
733-
734-
$widget._XhrKiller();
735-
$widget._EnableProductMediaLoader($this);
736-
$widget.xhr = $.post(
737-
$widget.options.mediaCallback, {
738-
'product_id': productId,
739-
attributes: attributes,
740-
isAjax: true,
741-
additional: additional
742-
}, function (data) {
743-
$widget._ProductMediaCallback($this, data);
744-
$widget._DisableProductMediaLoader($this);
745-
},
746-
'json'
747-
).done(function () {
743+
mediaCallData = {
744+
product_id: productId,
745+
attributes: attributes,
746+
additional: $.parseQuery()
747+
};
748+
mediaCacheKey = JSON.stringify(mediaCallData);
749+
750+
if (mediaCacheKey in $widget.options.mediaCache) {
751+
mediaSuccessCallback($widget.options.mediaCache[mediaCacheKey]);
752+
} else {
753+
mediaCallData.isAjax = true;
754+
$widget._XhrKiller();
755+
$widget._EnableProductMediaLoader($this);
756+
$widget.xhr = $.post(
757+
$widget.options.mediaCallback,
758+
mediaCallData,
759+
mediaSuccessCallback,
760+
'json'
761+
).done(function () {
748762
$widget._XhrKiller();
749763
});
764+
}
750765
},
751766

752767
/**
@@ -816,26 +831,28 @@ define([
816831
return;
817832
}
818833

819-
if (support(response)) {
820-
images.push({
821-
full: response.large,
822-
img: response.medium,
823-
thumb: response.small,
824-
isMain: true
825-
});
834+
if (!support(response)) {
835+
return;
836+
}
826837

827-
if (response.hasOwnProperty('gallery')) {
828-
$.each(response.gallery, function () {
829-
if (!support(this) || response.large === this.large) {
830-
return;
831-
}
832-
images.push({
833-
full: this.large,
834-
img: this.medium,
835-
thumb: this.small
836-
});
838+
images.push({
839+
full: response.large,
840+
img: response.medium,
841+
thumb: response.small,
842+
isMain: true
843+
});
844+
845+
if (response.hasOwnProperty('gallery')) {
846+
$.each(response.gallery, function () {
847+
if (!support(this) || response.large === this.large) {
848+
return;
849+
}
850+
images.push({
851+
full: this.large,
852+
img: this.medium,
853+
thumb: this.small
837854
});
838-
}
855+
});
839856
}
840857

841858
this.updateBaseImage(images, $main, isProductViewExist);
@@ -872,11 +889,8 @@ define([
872889
gallery = context.find(this.options.mediaGallerySelector).data('gallery'),
873890
item;
874891

875-
if (images) {
876-
imagesToUpdate = this._setImageType($.extend(true, [], images));
877-
}
878-
879892
if (isProductViewExist) {
893+
imagesToUpdate = images.length ? this._setImageType($.extend(true, [], images)) : [];
880894
if (this.options.onlyMainImg) {
881895
updateImg = imagesToUpdate.filter(function (img) {
882896
return img.isMain;

app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ define([
2323
},
2424
_bind: function() {
2525
var options = this.options,
26+
dataUpdateFunc = '_updateWishlistData',
2627
changeCustomOption = 'change ' + options.customOptionsInfo,
2728
changeQty = 'change ' + options.qtyInfo,
2829
events = {};
@@ -35,12 +36,14 @@ define([
3536
options.productType = [];
3637
}
3738

38-
events[changeCustomOption] = '_updateWishlistData';
39-
events[changeQty] = '_updateWishlistData';
40-
options.productType.forEach(function (type) {
41-
events['change ' + options[type + 'Info']] = '_updateWishlistData';
42-
});
39+
events[changeCustomOption] = dataUpdateFunc;
40+
events[changeQty] = dataUpdateFunc;
4341

42+
for (var key in options.productType) {
43+
if (options.productType.hasOwnProperty(key) && options.productType[key] + 'Info' in options) {
44+
events['change ' + options[options.productType[key] + 'Info']] = dataUpdateFunc;
45+
}
46+
}
4447
this._on(events);
4548
},
4649
_updateWishlistData: function(event) {

0 commit comments

Comments
 (0)