Skip to content

Commit 95a3136

Browse files
MAGETWO-94112: Configurable "As low As" Product Price Not Updating Correctly
1 parent c59e602 commit 95a3136

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-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: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,33 @@ 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+
503+
if (optionPriceDiff !== 0) {
504+
prices = {};
505+
priceValue = this._calculatePriceDifference(allowedProduct);
506+
}
490507
}
491508

492509
prices[element.attributeId] = priceValue;
@@ -495,6 +512,55 @@ define([
495512
return prices;
496513
},
497514

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

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,13 +974,29 @@ define([
974974
* @private
975975
*/
976976
_getPrices: function (newPrices, displayPrices) {
977-
var $widget = this;
977+
var $widget = this,
978+
optionPriceDiff = 0,
979+
allowedProduct, optionPrices, basePrice, optionFinalPrice;
978980

979981
if (_.isEmpty(newPrices)) {
980-
newPrices = $widget.options.jsonConfig.prices;
982+
allowedProduct = this._getAllowedProductWithMinPrice(this._CalcProducts());
983+
optionPrices = this.options.jsonConfig.optionPrices;
984+
basePrice = parseFloat(this.options.jsonConfig.prices.basePrice.amount);
985+
986+
if (!_.isEmpty(allowedProduct)) {
987+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
988+
optionPriceDiff = optionFinalPrice - basePrice;
989+
}
990+
991+
if (optionPriceDiff !== 0) {
992+
newPrices = this.options.jsonConfig.optionPrices[allowedProduct];
993+
} else {
994+
newPrices = $widget.options.jsonConfig.prices;
995+
}
981996
}
982997

983998
_.each(displayPrices, function (price, code) {
999+
9841000
if (newPrices[code]) {
9851001
displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
9861002
}
@@ -989,6 +1005,34 @@ define([
9891005
return displayPrices;
9901006
},
9911007

1008+
/**
1009+
* Get product with minimum price from selected options.
1010+
*
1011+
* @param {Array} allowedProducts
1012+
* @returns {String}
1013+
* @private
1014+
*/
1015+
_getAllowedProductWithMinPrice: function (allowedProducts) {
1016+
var optionPrices = this.options.jsonConfig.optionPrices,
1017+
product = {},
1018+
optionFinalPrice, optionMinPrice;
1019+
1020+
_.each(allowedProducts, function (allowedProduct) {
1021+
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
1022+
1023+
if (_.isEmpty(product)) {
1024+
optionMinPrice = optionFinalPrice;
1025+
product = allowedProduct;
1026+
}
1027+
1028+
if (optionFinalPrice < optionMinPrice) {
1029+
product = allowedProduct;
1030+
}
1031+
}, this);
1032+
1033+
return product;
1034+
},
1035+
9921036
/**
9931037
* Gets all product media and change current to the needed one
9941038
*

0 commit comments

Comments
 (0)