Skip to content

Fix #7200 - Configurable Products dropdown not showing pricing #24255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -123,6 +123,8 @@ define([
if (this.options.spConfig.inputsInitialized) {
this._setValuesByAttribute();
}

this._setInitialOptionsLabels();
},

/**
Expand Down Expand Up @@ -158,6 +160,18 @@ 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 @@ -371,6 +385,8 @@ define([
prevConfig,
index = 1,
allowedProducts,
allowedProductsByOption,
allowedProductsAll,
i,
j,
finalPrice = parseFloat(this.options.spConfig.prices.finalPrice.amount),
Expand All @@ -379,7 +395,8 @@ define([
optionPrices = this.options.spConfig.optionPrices,
allowedOptions = [],
indexKey,
allowedProductMinPrice;
allowedProductMinPrice,
allowedProductsAllMinPrice;

this._clearSelect(element);
element.options[0] = new Option('', '');
Expand All @@ -398,35 +415,50 @@ define([
}
}

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

/* eslint-disable max-depth */
if (prevConfig) {
for (i = 0; i < options.length; i++) {
/* eslint-disable max-depth */
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) {
allowedProducts.push(options[i].products[j]);
if (!allowedProductsByOption[i]) {
allowedProductsByOption[i] = [];
}
allowedProductsByOption[i].push(options[i].products[j]);
allowedProductsAll.push(options[i].products[j]);
}
}
} 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 - finalPrice;

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

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
);
}
}

Expand Down