Skip to content

Commit f349c0e

Browse files
committed
Fix getting product id
Fix checking if swatch is used in product view or list view
1 parent 5639790 commit f349c0e

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,12 @@ define([
309309
_create: function () {
310310
var options = this.options,
311311
gallery = $('[data-gallery-role=gallery-placeholder]', '.column.main'),
312-
isProductViewExist = $('body.catalog-product-view').size() > 0,
313-
$main = isProductViewExist ?
312+
productData = this._determineProductData(),
313+
$main = productData.isInProductView ?
314314
this.element.parents('.column.main') :
315315
this.element.parents('.product-item-info');
316316

317-
if (isProductViewExist) {
317+
if (productData.isInProductView) {
318318
gallery.data('gallery') ?
319319
this._onGalleryLoaded(gallery) :
320320
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));
@@ -328,6 +328,28 @@ define([
328328
this.inProductList = this.productForm.length > 0;
329329
},
330330

331+
/**
332+
* Determine product id and related data
333+
*
334+
* @returns {{productId: *, isInProductView: boolean}}
335+
* @private
336+
*/
337+
_determineProductData: function() {
338+
// Check if product is in a list of products.
339+
var productId = this.element.parents('.product-item-details')
340+
.find('.price-box.price-final_price').attr('data-product-id'),
341+
isInProductView = false;
342+
343+
if (!productId) {
344+
// Check individual product.
345+
var product = document.getElementsByName('product')[0];
346+
productId = product ? product.value : undefined;
347+
isInProductView = productId > 0;
348+
}
349+
350+
return {productId: productId, isInProductView: isInProductView};
351+
},
352+
331353
/**
332354
* Render controls
333355
*
@@ -897,7 +919,7 @@ define([
897919
var $widget = this,
898920
$this = $widget.element,
899921
attributes = {},
900-
productId = 0,
922+
productData = this._determineProductData(),
901923
mediaCallData,
902924
mediaCacheKey,
903925

@@ -911,7 +933,7 @@ define([
911933
if (!(mediaCacheKey in $widget.options.mediaCache)) {
912934
$widget.options.mediaCache[mediaCacheKey] = data;
913935
}
914-
$widget._ProductMediaCallback($this, data);
936+
$widget._ProductMediaCallback($this, data, productData.isInProductView);
915937
$widget._DisableProductMediaLoader($this);
916938
};
917939

@@ -925,17 +947,8 @@ define([
925947
attributes[$selected.attr('attribute-code')] = $selected.attr('option-selected');
926948
});
927949

928-
if ($('body.catalog-product-view').size() > 0) {
929-
//Product Page
930-
productId = document.getElementsByName('product')[0].value;
931-
} else {
932-
//Category View
933-
productId = $this.parents('.product.details.product-item-details')
934-
.find('.price-box.price-final_price').attr('data-product-id');
935-
}
936-
937950
mediaCallData = {
938-
'product_id': productId,
951+
'product_id': productData.productId,
939952
'attributes': attributes,
940953
'additional': $.parseQuery()
941954
};
@@ -1001,11 +1014,11 @@ define([
10011014
*
10021015
* @param {Object} $this
10031016
* @param {String} response
1017+
* @param {Boolean} isInProductView
10041018
* @private
10051019
*/
1006-
_ProductMediaCallback: function ($this, response) {
1007-
var isProductViewExist = $('body.catalog-product-view').size() > 0,
1008-
$main = isProductViewExist ? $this.parents('.column.main') : $this.parents('.product-item-info'),
1020+
_ProductMediaCallback: function ($this, response, isInProductView) {
1021+
var $main = isInProductView ? $this.parents('.column.main') : $this.parents('.product-item-info'),
10091022
$widget = this,
10101023
images = [],
10111024

@@ -1020,7 +1033,7 @@ define([
10201033
};
10211034

10221035
if (_.size($widget) < 1 || !support(response)) {
1023-
this.updateBaseImage(this.options.mediaGalleryInitial, $main, isProductViewExist);
1036+
this.updateBaseImage(this.options.mediaGalleryInitial, $main, isInProductView);
10241037

10251038
return;
10261039
}
@@ -1045,7 +1058,7 @@ define([
10451058
});
10461059
}
10471060

1048-
this.updateBaseImage(images, $main, isProductViewExist);
1061+
this.updateBaseImage(images, $main, isInProductView);
10491062
},
10501063

10511064
/**
@@ -1070,16 +1083,16 @@ define([
10701083
* Update [gallery-placeholder] or [product-image-photo]
10711084
* @param {Array} images
10721085
* @param {jQuery} context
1073-
* @param {Boolean} isProductViewExist
1086+
* @param {Boolean} isInProductView
10741087
*/
1075-
updateBaseImage: function (images, context, isProductViewExist) {
1088+
updateBaseImage: function (images, context, isInProductView) {
10761089
var justAnImage = images[0],
10771090
initialImages = this.options.mediaGalleryInitial,
10781091
gallery = context.find(this.options.mediaGallerySelector).data('gallery'),
10791092
imagesToUpdate,
10801093
isInitial;
10811094

1082-
if (isProductViewExist) {
1095+
if (isInProductView) {
10831096
imagesToUpdate = images.length ? this._setImageType($.extend(true, [], images)) : [];
10841097
isInitial = _.isEqual(imagesToUpdate, initialImages);
10851098

0 commit comments

Comments
 (0)