Skip to content

Commit

Permalink
Fix getting product id
Browse files Browse the repository at this point in the history
Fix checking if swatch is used in product view or list view
  • Loading branch information
mimarcel committed Feb 9, 2017
1 parent 5639790 commit f349c0e
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ define([
_create: function () {
var options = this.options,
gallery = $('[data-gallery-role=gallery-placeholder]', '.column.main'),
isProductViewExist = $('body.catalog-product-view').size() > 0,
$main = isProductViewExist ?
productData = this._determineProductData(),
$main = productData.isInProductView ?
this.element.parents('.column.main') :
this.element.parents('.product-item-info');

if (isProductViewExist) {
if (productData.isInProductView) {
gallery.data('gallery') ?
this._onGalleryLoaded(gallery) :
gallery.on('gallery:loaded', this._onGalleryLoaded.bind(this, gallery));
Expand All @@ -328,6 +328,28 @@ define([
this.inProductList = this.productForm.length > 0;
},

/**
* Determine product id and related data
*
* @returns {{productId: *, isInProductView: boolean}}
* @private
*/
_determineProductData: function() {
// Check if product is in a list of products.
var productId = this.element.parents('.product-item-details')
.find('.price-box.price-final_price').attr('data-product-id'),
isInProductView = false;

if (!productId) {
// Check individual product.
var product = document.getElementsByName('product')[0];
productId = product ? product.value : undefined;
isInProductView = productId > 0;
}

return {productId: productId, isInProductView: isInProductView};
},

/**
* Render controls
*
Expand Down Expand Up @@ -897,7 +919,7 @@ define([
var $widget = this,
$this = $widget.element,
attributes = {},
productId = 0,
productData = this._determineProductData(),
mediaCallData,
mediaCacheKey,

Expand All @@ -911,7 +933,7 @@ define([
if (!(mediaCacheKey in $widget.options.mediaCache)) {
$widget.options.mediaCache[mediaCacheKey] = data;
}
$widget._ProductMediaCallback($this, data);
$widget._ProductMediaCallback($this, data, productData.isInProductView);
$widget._DisableProductMediaLoader($this);
};

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

if ($('body.catalog-product-view').size() > 0) {
//Product Page
productId = document.getElementsByName('product')[0].value;
} else {
//Category View
productId = $this.parents('.product.details.product-item-details')
.find('.price-box.price-final_price').attr('data-product-id');
}

mediaCallData = {
'product_id': productId,
'product_id': productData.productId,
'attributes': attributes,
'additional': $.parseQuery()
};
Expand Down Expand Up @@ -1001,11 +1014,11 @@ define([
*
* @param {Object} $this
* @param {String} response
* @param {Boolean} isInProductView
* @private
*/
_ProductMediaCallback: function ($this, response) {
var isProductViewExist = $('body.catalog-product-view').size() > 0,
$main = isProductViewExist ? $this.parents('.column.main') : $this.parents('.product-item-info'),
_ProductMediaCallback: function ($this, response, isInProductView) {
var $main = isInProductView ? $this.parents('.column.main') : $this.parents('.product-item-info'),
$widget = this,
images = [],

Expand All @@ -1020,7 +1033,7 @@ define([
};

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

return;
}
Expand All @@ -1045,7 +1058,7 @@ define([
});
}

this.updateBaseImage(images, $main, isProductViewExist);
this.updateBaseImage(images, $main, isInProductView);
},

/**
Expand All @@ -1070,16 +1083,16 @@ define([
* Update [gallery-placeholder] or [product-image-photo]
* @param {Array} images
* @param {jQuery} context
* @param {Boolean} isProductViewExist
* @param {Boolean} isInProductView
*/
updateBaseImage: function (images, context, isProductViewExist) {
updateBaseImage: function (images, context, isInProductView) {
var justAnImage = images[0],
initialImages = this.options.mediaGalleryInitial,
gallery = context.find(this.options.mediaGallerySelector).data('gallery'),
imagesToUpdate,
isInitial;

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

Expand Down

0 comments on commit f349c0e

Please sign in to comment.