Skip to content

Commit 3724b5c

Browse files
author
Bomko, Alex(abomko)
committed
Merge pull request #2 from magento-goinc/MAGETWO-44825
MAGETWO-44825 Swatches does not update configurable product price
2 parents 4621cdf + b35b3b2 commit 3724b5c

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

app/code/Magento/Swatches/view/frontend/web/js/SwatchRenderer.js

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22
* Copyright © 2015 Magento. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5-
Number.prototype.formatMoney = function (c, d, t) { //this function helps format price
6-
var n = this,
7-
c = isNaN(c = Math.abs(c)) ? 2 : c,
8-
d = d == undefined ? "." : d,
9-
t = t == undefined ? "," : t,
10-
s = n < 0 ? "-" : "",
11-
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
12-
j = (j = i.length) > 3 ? j % 3 : 0;
13-
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
14-
};
15-
16-
Array.prototype.intersection = function (a) {
17-
return this.filter(function (i) {
18-
return a.indexOf(i) >= 0;
19-
});
20-
};
215

22-
define(["jquery", "jquery/ui"], function ($) {
6+
define(['jquery', 'underscore', 'jquery/ui'], function ($, _) {
7+
'use strict';
8+
9+
/**
10+
* Parse params
11+
* @param {String} query
12+
* @returns {{}}
13+
*/
2314
$.parseParams = function (query) {
2415
var re = /([^&=]+)=?([^&]*)/g,
2516
decodeRE = /\+/g, // Regex for replacing addition symbol with a space
@@ -176,7 +167,7 @@ define(["jquery", "jquery/ui"], function ($) {
176167
jsonConfig: {}, // option's json config
177168
jsonSwatchConfig: {}, // swatch's json config
178169
selectorProduct: '.product-info-main', // selector of parental block of prices and swatches (need to know where to seek for price block)
179-
selectorProductPrice: '.price-final-price .price', // selector of price wrapper (need to know where set price)
170+
selectorProductPrice: '[data-role=priceBox]', // selector of price wrapper (need to know where set price)
180171
numberToShow: false, // number of controls to show (false or zero = show all)
181172
onlySwatches: false, // show only swatch controls
182173
enableControlLabel: true, // enable label for control
@@ -476,7 +467,13 @@ define(["jquery", "jquery/ui"], function ($) {
476467
}
477468

478469
$widget._Rebuild();
479-
$widget._UpdatePrice();
470+
471+
if ($widget.element.parents($widget.options.selectorProduct)
472+
.find(this.options.selectorProductPrice).is(':data(mage-priceBox)')
473+
) {
474+
$widget._UpdatePrice();
475+
}
476+
480477
$widget._LoadProductMedia();
481478
},
482479

@@ -564,7 +561,7 @@ define(["jquery", "jquery/ui"], function ($) {
564561
return;
565562
}
566563

567-
if (products.intersection($widget.optionsMap[id][option].products).length <= 0) {
564+
if (_.intersection(products, $widget.optionsMap[id][option].products).length <= 0) {
568565
$this.attr('disabled', true).addClass('disabled');
569566
}
570567
});
@@ -597,7 +594,7 @@ define(["jquery", "jquery/ui"], function ($) {
597594
if (products.length == 0) {
598595
products = $widget.optionsMap[id][option].products;
599596
} else {
600-
products = products.intersection($widget.optionsMap[id][option].products);
597+
products = _.intersection(products, $widget.optionsMap[id][option].products);
601598
}
602599
});
603600

@@ -612,21 +609,48 @@ define(["jquery", "jquery/ui"], function ($) {
612609
_UpdatePrice: function () {
613610
var $widget = this,
614611
$product = $widget.element.parents($widget.options.selectorProduct),
615-
price = $product.find('[data-price-amount]').data('price-amount');
612+
$productPrice = $product.find(this.options.selectorProductPrice),
613+
options = _.object(_.keys($widget.optionsMap), {}),
614+
result;
616615

617616
$widget.element.find('.' + $widget.options.classes.attributeClass + '[option-selected]').each(function () {
618-
var id = $(this).attr('attribute-id');
619-
var option = $(this).attr('option-selected');
617+
var attributeId = $(this).attr('attribute-id'),
618+
selectedOptionId = $(this).attr('option-selected');
619+
620+
options[attributeId] = selectedOptionId;
621+
});
622+
623+
result = $widget.options.jsonConfig.optionPrices[_.findKey($widget.options.jsonConfig.index, options)];
620624

621-
price = $widget.optionsMap[id][option].price;
625+
$productPrice.trigger(
626+
'updatePrice',
627+
{
628+
'prices': $widget._getPrices(result, $productPrice.priceBox('option').prices)
629+
}
630+
);
631+
632+
},
633+
634+
/**
635+
* Get prices
636+
* @param {Object} newPrices
637+
* @returns {Object}
638+
* @private
639+
*/
640+
_getPrices: function (newPrices, displayPrices) {
641+
var $widget = this;
642+
643+
if (_.isEmpty(newPrices)) {
644+
newPrices = $widget.options.jsonConfig.prices;
645+
}
646+
647+
_.each(displayPrices, function (price, code) {
648+
if (newPrices[code]) {
649+
displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
650+
}
622651
});
623652

624-
$product
625-
.find($widget.options.selectorProductPrice)
626-
.text($widget.options.jsonConfig.template.replace(
627-
'<%- data.price %>',
628-
price.formatMoney(2)
629-
));
653+
return displayPrices;
630654
},
631655

632656
/**

0 commit comments

Comments
 (0)