Skip to content

Commit

Permalink
Merge branch 'MAGETWO-94112' into 2.2-develop-pr11
Browse files Browse the repository at this point in the history
  • Loading branch information
rostyslav-hymon committed Nov 27, 2018
2 parents e194edc + 95a3136 commit 2185923
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/view/base/web/js/price-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ define([
pricesCode = [],
priceValue, origin, finalPrice;

this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
if (typeof newPrices !== 'undefined' && newPrices.hasOwnProperty('prices')) {
this.cache.additionalPriceObject = {};
} else {
this.cache.additionalPriceObject = this.cache.additionalPriceObject || {};
}

if (newPrices) {
$.extend(this.cache.additionalPriceObject, newPrices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,33 @@ define([
_getPrices: function () {
var prices = {},
elements = _.toArray(this.options.settings),
hasProductPrice = false;
hasProductPrice = false,
optionPriceDiff = 0,
allowedProduct, optionPrices, basePrice, optionFinalPrice;

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

if (config && config.allowedProducts.length === 1 && !hasProductPrice) {
prices = {};
priceValue = this._calculatePrice(config);
hasProductPrice = true;
} else if (element.value) {
allowedProduct = this._getAllowedProductWithMinPrice(config.allowedProducts);
optionPrices = this.options.spConfig.optionPrices;
basePrice = parseFloat(this.options.spConfig.prices.basePrice.amount);

if (!_.isEmpty(allowedProduct)) {
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
optionPriceDiff = optionFinalPrice - basePrice;
}

if (optionPriceDiff !== 0) {
prices = {};
priceValue = this._calculatePriceDifference(allowedProduct);
}
}

prices[element.attributeId] = priceValue;
Expand All @@ -495,6 +512,55 @@ define([
return prices;
},

/**
* Get product with minimum price from selected options.
*
* @param {Array} allowedProducts
* @returns {String}
* @private
*/
_getAllowedProductWithMinPrice: function (allowedProducts) {
var optionPrices = this.options.spConfig.optionPrices,
product = {},
optionMinPrice, optionFinalPrice;

_.each(allowedProducts, function (allowedProduct) {
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);

if (_.isEmpty(product)) {
optionMinPrice = optionFinalPrice;
product = allowedProduct;
}

if (optionFinalPrice < optionMinPrice) {
product = allowedProduct;
}
}, this);

return product;
},

/**
* Calculate price difference for allowed product
*
* @param {*} allowedProduct - Product
* @returns {*}
* @private
*/
_calculatePriceDifference: function (allowedProduct) {
var displayPrices = $(this.options.priceHolderSelector).priceBox('option').prices,
newPrices = this.options.spConfig.optionPrices[allowedProduct];

_.each(displayPrices, function (price, code) {

if (newPrices[code]) {
displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
}
});

return displayPrices;
},

/**
* Returns prices for configured products
*
Expand Down
48 changes: 46 additions & 2 deletions app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,13 +974,29 @@ define([
* @private
*/
_getPrices: function (newPrices, displayPrices) {
var $widget = this;
var $widget = this,
optionPriceDiff = 0,
allowedProduct, optionPrices, basePrice, optionFinalPrice;

if (_.isEmpty(newPrices)) {
newPrices = $widget.options.jsonConfig.prices;
allowedProduct = this._getAllowedProductWithMinPrice(this._CalcProducts());
optionPrices = this.options.jsonConfig.optionPrices;
basePrice = parseFloat(this.options.jsonConfig.prices.basePrice.amount);

if (!_.isEmpty(allowedProduct)) {
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);
optionPriceDiff = optionFinalPrice - basePrice;
}

if (optionPriceDiff !== 0) {
newPrices = this.options.jsonConfig.optionPrices[allowedProduct];
} else {
newPrices = $widget.options.jsonConfig.prices;
}
}

_.each(displayPrices, function (price, code) {

if (newPrices[code]) {
displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
}
Expand All @@ -989,6 +1005,34 @@ define([
return displayPrices;
},

/**
* Get product with minimum price from selected options.
*
* @param {Array} allowedProducts
* @returns {String}
* @private
*/
_getAllowedProductWithMinPrice: function (allowedProducts) {
var optionPrices = this.options.jsonConfig.optionPrices,
product = {},
optionFinalPrice, optionMinPrice;

_.each(allowedProducts, function (allowedProduct) {
optionFinalPrice = parseFloat(optionPrices[allowedProduct].finalPrice.amount);

if (_.isEmpty(product)) {
optionMinPrice = optionFinalPrice;
product = allowedProduct;
}

if (optionFinalPrice < optionMinPrice) {
product = allowedProduct;
}
}, this);

return product;
},

/**
* Gets all product media and change current to the needed one
*
Expand Down

0 comments on commit 2185923

Please sign in to comment.