Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ define([
state: {},
priceFormat: {},
optionTemplate: '<%- data.label %>' +
'<% if (typeof data.finalPrice.value !== "undefined") { %>' +
' <%- data.finalPrice.formatted %>' +
'<% } %>',
'<% if (typeof data.finalPrice.value !== "undefined") { %>' +
' <%- data.finalPrice.formatted %>' +
'<% } %>',
mediaGallerySelector: '[data-gallery-role=gallery-placeholder]',
mediaGalleryInitial: null,
slyOldPriceSelector: '.sly-old-price',
Expand Down Expand Up @@ -123,8 +123,6 @@ define([
if (this.options.spConfig.inputsInitialized) {
this._setValuesByAttribute();
}

this._setInitialOptionsLabels();
},

/**
Expand Down Expand Up @@ -160,18 +158,6 @@ define([
}, this));
},

/**
* Set additional field with initial label to be used when switching between options with different prices.
* @private
*/
_setInitialOptionsLabels: function () {
$.each(this.options.spConfig.attributes, $.proxy(function (index, element) {
$.each(element.options, $.proxy(function (optIndex, optElement) {
this.options.spConfig.attributes[index].options[optIndex].initialLabel = optElement.label;
}, this));
}, this));
},

/**
* Set up .on('change') events for each option element to configure the option.
* @private
Expand Down Expand Up @@ -385,18 +371,13 @@ define([
prevConfig,
index = 1,
allowedProducts,
allowedProductsByOption,
allowedProductsAll,
i,
j,
finalPrice = parseFloat(this.options.spConfig.prices.finalPrice.amount),
basePrice = parseFloat(this.options.spConfig.prices.basePrice.amount),
optionFinalPrice,
optionPriceDiff,
optionPrices = this.options.spConfig.optionPrices,
allowedOptions = [],
indexKey,
allowedProductMinPrice,
allowedProductsAllMinPrice;
allowedProductMinPrice;

this._clearSelect(element);
element.options[0] = new Option('', '');
Expand All @@ -408,78 +389,60 @@ define([
}

if (options) {
for (indexKey in this.options.spConfig.index) {
/* eslint-disable max-depth */
if (this.options.spConfig.index.hasOwnProperty(indexKey)) {
allowedOptions = allowedOptions.concat(_.values(this.options.spConfig.index[indexKey]));
}
}

if (prevConfig) {
allowedProductsByOption = {};
allowedProductsAll = [];
for (i = 0; i < options.length; i++) {
allowedProducts = [];
optionPriceDiff = 0;

for (i = 0; i < options.length; i++) {
/* eslint-disable max-depth */
/* eslint-disable max-depth */
if (prevConfig) {
for (j = 0; j < options[i].products.length; j++) {
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1) {
if (!allowedProductsByOption[i]) {
allowedProductsByOption[i] = [];
}
allowedProductsByOption[i].push(options[i].products[j]);
allowedProductsAll.push(options[i].products[j]);
allowedProducts.push(options[i].products[j]);
}
}
}

if (typeof allowedProductsAll[0] !== 'undefined' &&
typeof optionPrices[allowedProductsAll[0]] !== 'undefined') {
allowedProductsAllMinPrice = this._getAllowedProductWithMinPrice(allowedProductsAll);
finalPrice = parseFloat(optionPrices[allowedProductsAllMinPrice].finalPrice.amount);
}
}

for (i = 0; i < options.length; i++) {
allowedProducts = prevConfig ? allowedProductsByOption[i] : options[i].products.slice(0);
optionPriceDiff = 0;

if (typeof allowedProducts[0] !== 'undefined' &&
typeof optionPrices[allowedProducts[0]] !== 'undefined') {
allowedProductMinPrice = this._getAllowedProductWithMinPrice(allowedProducts);
optionFinalPrice = parseFloat(optionPrices[allowedProductMinPrice].finalPrice.amount);
optionPriceDiff = optionFinalPrice - finalPrice;
options[i].label = options[i].initialLabel;

if (optionPriceDiff !== 0) {
options[i].label += ' ' + priceUtils.formatPrice(
optionPriceDiff,
this.options.priceFormat,
true
);
} else {
allowedProducts = options[i].products.slice(0);

if (typeof allowedProducts[0] !== 'undefined' &&
typeof optionPrices[allowedProducts[0]] !== 'undefined') {
allowedProductMinPrice = this._getAllowedProductWithMinPrice(allowedProducts);
optionFinalPrice = parseFloat(optionPrices[allowedProductMinPrice].finalPrice.amount);
optionPriceDiff = optionFinalPrice - basePrice;

if (optionPriceDiff !== 0) {
options[i].label = options[i].label + ' ' + priceUtils.formatPrice(
optionPriceDiff,
this.options.priceFormat,
true);
}
}
}

if (allowedProducts.length > 0 || _.include(allowedOptions, options[i].id)) {
if (allowedProducts.length > 0) {
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);

if (typeof options[i].price !== 'undefined') {
element.options[index].setAttribute('price', options[i].price);
}

if (allowedProducts.length === 0) {
element.options[index].disabled = true;
}

element.options[index].config = options[i];
index++;
}

if (i === 0) {
this.options.values[attributeId] = options[i].id;
}
/* eslint-enable max-depth */
}

if (window.location.href.indexOf('#') !== -1) {
this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
}

}
},

Expand Down
Loading