Skip to content

Commit 329c590

Browse files
MAGETWO-96308: Configurable "As low As" Product Price Not Updating Correctly
1 parent 365e400 commit 329c590

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

app/code/Magento/Catalog/view/base/web/js/price-box.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ define([
7878
pricesCode = [],
7979
priceValue, origin, finalPrice;
8080

81-
this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
81+
if (typeof newPrices !== 'undefined' && newPrices.hasOwnProperty("prices")) {
82+
this.cache.additionalPriceObject = {};
83+
} else {
84+
this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
85+
}
8286

8387
if (newPrices) {
8488
$.extend(this.cache.additionalPriceObject, newPrices);

app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,32 @@ define([
477477
_getPrices: function () {
478478
var prices = {},
479479
elements = _.toArray(this.options.settings),
480-
hasProductPrice = false;
480+
hasProductPrice = false,
481+
optionPriceDiff = 0,
482+
allowedProduct, optionPrices, basePrice, optionFinalPrice;
481483

482484
_.each(elements, function (element) {
483485
var selected = element.options[element.selectedIndex],
484486
config = selected && selected.config,
485487
priceValue = {};
486488

487489
if (config && config.allowedProducts.length === 1 && !hasProductPrice) {
490+
prices = {};
488491
priceValue = this._calculatePrice(config);
489492
hasProductPrice = true;
493+
} else if (element.value) {
494+
allowedProduct = this._getAllowedProductWithMinPrice(config.allowedProducts);
495+
optionPrices = this.options.spConfig.optionPrices;
496+
basePrice = parseFloat(this.options.spConfig.prices.basePrice.amount);
497+
498+
if (!_.isEmpty(allowedProduct)) {
499+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
500+
optionPriceDiff = optionFinalPrice - basePrice;
501+
}
502+
if (optionPriceDiff !== 0) {
503+
prices = {};
504+
priceValue = this._calculatePriceDifference(allowedProduct);
505+
}
490506
}
491507

492508
prices[element.attributeId] = priceValue;
@@ -495,6 +511,53 @@ define([
495511
return prices;
496512
},
497513

514+
/**
515+
* Get product with minimum price from selected options.
516+
*
517+
* @param {Array} allowedProducts
518+
* @returns {String}
519+
* @private
520+
*/
521+
_getAllowedProductWithMinPrice: function (allowedProducts) {
522+
var optionPrices = this.options.spConfig.optionPrices,
523+
product = {},
524+
optionMinPrice, optionFinalPrice;
525+
526+
_.each(allowedProducts, function (allowedProduct) {
527+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
528+
529+
if (_.isEmpty(product)) {
530+
optionMinPrice = optionFinalPrice;
531+
product = allowedProduct;
532+
}
533+
if (optionFinalPrice < optionMinPrice) {
534+
product = allowedProduct;
535+
}
536+
}, this);
537+
538+
return product;
539+
},
540+
541+
/**
542+
* Calculate price difference for allowed product
543+
*
544+
* @param {*} allowedProduct - Product
545+
* @returns {*}
546+
* @private
547+
*/
548+
_calculatePriceDifference: function (allowedProduct) {
549+
var displayPrices = $(this.options.priceHolderSelector).priceBox('option').prices,
550+
newPrices = this.options.spConfig.optionPrices[allowedProduct];
551+
552+
_.each(displayPrices, function (price, code) {
553+
if (newPrices[code]) {
554+
displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
555+
}
556+
});
557+
558+
return displayPrices;
559+
},
560+
498561
/**
499562
* Returns prices for configured products
500563
*

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,24 @@ define([
968968
* @private
969969
*/
970970
_getPrices: function (newPrices, displayPrices) {
971-
var $widget = this;
971+
var $widget = this,
972+
optionPriceDiff = 0,
973+
allowedProduct, optionPrices, basePrice, optionFinalPrice;
972974

973975
if (_.isEmpty(newPrices)) {
974-
newPrices = $widget.options.jsonConfig.prices;
976+
allowedProduct = this._getAllowedProductWithMinPrice(this._CalcProducts());
977+
optionPrices = this.options.jsonConfig.optionPrices;
978+
basePrice = parseFloat(this.options.jsonConfig.prices.basePrice.amount);
979+
980+
if (!_.isEmpty(allowedProduct)) {
981+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
982+
optionPriceDiff = optionFinalPrice - basePrice;
983+
}
984+
if (optionPriceDiff !== 0) {
985+
newPrices = this.options.jsonConfig.optionPrices[allowedProduct];
986+
} else {
987+
newPrices = $widget.options.jsonConfig.prices;
988+
}
975989
}
976990

977991
_.each(displayPrices, function (price, code) {
@@ -983,6 +997,33 @@ define([
983997
return displayPrices;
984998
},
985999

1000+
/**
1001+
* Get product with minimum price from selected options.
1002+
*
1003+
* @param {Array} allowedProducts
1004+
* @returns {String}
1005+
* @private
1006+
*/
1007+
_getAllowedProductWithMinPrice: function (allowedProducts) {
1008+
var optionPrices = this.options.jsonConfig.optionPrices,
1009+
product = {},
1010+
optionFinalPrice, optionMinPrice;
1011+
1012+
_.each(allowedProducts, function (allowedProduct) {
1013+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
1014+
1015+
if (_.isEmpty(product)) {
1016+
optionMinPrice = optionFinalPrice;
1017+
product = allowedProduct;
1018+
}
1019+
if (optionFinalPrice < optionMinPrice) {
1020+
product = allowedProduct;
1021+
}
1022+
}, this);
1023+
1024+
return product;
1025+
},
1026+
9861027
/**
9871028
* Gets all product media and change current to the needed one
9881029
*

0 commit comments

Comments
 (0)