2
2
* Copyright © 2015 Magento. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
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
- } ;
21
5
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
+ */
23
14
$ . parseParams = function ( query ) {
24
15
var re = / ( [ ^ & = ] + ) = ? ( [ ^ & ] * ) / g,
25
16
decodeRE = / \+ / g, // Regex for replacing addition symbol with a space
@@ -176,7 +167,7 @@ define(["jquery", "jquery/ui"], function ($) {
176
167
jsonConfig : { } , // option's json config
177
168
jsonSwatchConfig : { } , // swatch's json config
178
169
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)
180
171
numberToShow : false , // number of controls to show (false or zero = show all)
181
172
onlySwatches : false , // show only swatch controls
182
173
enableControlLabel : true , // enable label for control
@@ -476,7 +467,13 @@ define(["jquery", "jquery/ui"], function ($) {
476
467
}
477
468
478
469
$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
+
480
477
$widget . _LoadProductMedia ( ) ;
481
478
} ,
482
479
@@ -564,7 +561,7 @@ define(["jquery", "jquery/ui"], function ($) {
564
561
return ;
565
562
}
566
563
567
- if ( products . intersection ( $widget . optionsMap [ id ] [ option ] . products ) . length <= 0 ) {
564
+ if ( _ . intersection ( products , $widget . optionsMap [ id ] [ option ] . products ) . length <= 0 ) {
568
565
$this . attr ( 'disabled' , true ) . addClass ( 'disabled' ) ;
569
566
}
570
567
} ) ;
@@ -597,7 +594,7 @@ define(["jquery", "jquery/ui"], function ($) {
597
594
if ( products . length == 0 ) {
598
595
products = $widget . optionsMap [ id ] [ option ] . products ;
599
596
} else {
600
- products = products . intersection ( $widget . optionsMap [ id ] [ option ] . products ) ;
597
+ products = _ . intersection ( products , $widget . optionsMap [ id ] [ option ] . products ) ;
601
598
}
602
599
} ) ;
603
600
@@ -612,21 +609,48 @@ define(["jquery", "jquery/ui"], function ($) {
612
609
_UpdatePrice : function ( ) {
613
610
var $widget = this ,
614
611
$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 ;
616
615
617
616
$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 ) ] ;
620
624
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
+ }
622
651
} ) ;
623
652
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 ;
630
654
} ,
631
655
632
656
/**
0 commit comments