@@ -123,6 +123,8 @@ define([
123
123
if ( this . options . spConfig . inputsInitialized ) {
124
124
this . _setValuesByAttribute ( ) ;
125
125
}
126
+
127
+ this . _setInitialOptionsLabels ( ) ;
126
128
} ,
127
129
128
130
/**
@@ -158,6 +160,18 @@ define([
158
160
} , this ) ) ;
159
161
} ,
160
162
163
+ /**
164
+ * Set additional field with initial label to be used when switching between options with different prices.
165
+ * @private
166
+ */
167
+ _setInitialOptionsLabels : function ( ) {
168
+ $ . each ( this . options . spConfig . attributes , $ . proxy ( function ( index , element ) {
169
+ $ . each ( element . options , $ . proxy ( function ( optIndex , optElement ) {
170
+ this . options . spConfig . attributes [ index ] . options [ optIndex ] . initialLabel = optElement . label ;
171
+ } , this ) ) ;
172
+ } , this ) ) ;
173
+ } ,
174
+
161
175
/**
162
176
* Set up .on('change') events for each option element to configure the option.
163
177
* @private
@@ -371,6 +385,8 @@ define([
371
385
prevConfig ,
372
386
index = 1 ,
373
387
allowedProducts ,
388
+ allowedProductsByOption ,
389
+ allowedProductsAll ,
374
390
i ,
375
391
j ,
376
392
finalPrice = parseFloat ( this . options . spConfig . prices . finalPrice . amount ) ,
@@ -379,7 +395,8 @@ define([
379
395
optionPrices = this . options . spConfig . optionPrices ,
380
396
allowedOptions = [ ] ,
381
397
indexKey ,
382
- allowedProductMinPrice ;
398
+ allowedProductMinPrice ,
399
+ allowedProductsAllMinPrice ;
383
400
384
401
this . _clearSelect ( element ) ;
385
402
element . options [ 0 ] = new Option ( '' , '' ) ;
@@ -398,35 +415,50 @@ define([
398
415
}
399
416
}
400
417
401
- for ( i = 0 ; i < options . length ; i ++ ) {
402
- allowedProducts = [ ] ;
403
- optionPriceDiff = 0 ;
418
+ if ( prevConfig ) {
419
+ allowedProductsByOption = { } ;
420
+ allowedProductsAll = [ ] ;
404
421
405
- /* eslint-disable max-depth */
406
- if ( prevConfig ) {
422
+ for ( i = 0 ; i < options . length ; i ++ ) {
423
+ /* eslint-disable max-depth */
407
424
for ( j = 0 ; j < options [ i ] . products . length ; j ++ ) {
408
425
// prevConfig.config can be undefined
409
426
if ( prevConfig . config &&
410
427
prevConfig . config . allowedProducts &&
411
428
prevConfig . config . allowedProducts . indexOf ( options [ i ] . products [ j ] ) > - 1 ) {
412
- allowedProducts . push ( options [ i ] . products [ j ] ) ;
429
+ if ( ! allowedProductsByOption [ i ] ) {
430
+ allowedProductsByOption [ i ] = [ ] ;
431
+ }
432
+ allowedProductsByOption [ i ] . push ( options [ i ] . products [ j ] ) ;
433
+ allowedProductsAll . push ( options [ i ] . products [ j ] ) ;
413
434
}
414
435
}
415
- } else {
416
- allowedProducts = options [ i ] . products . slice ( 0 ) ;
417
-
418
- if ( typeof allowedProducts [ 0 ] !== 'undefined' &&
419
- typeof optionPrices [ allowedProducts [ 0 ] ] !== 'undefined' ) {
420
- allowedProductMinPrice = this . _getAllowedProductWithMinPrice ( allowedProducts ) ;
421
- optionFinalPrice = parseFloat ( optionPrices [ allowedProductMinPrice ] . finalPrice . amount ) ;
422
- optionPriceDiff = optionFinalPrice - finalPrice ;
423
-
424
- if ( optionPriceDiff !== 0 ) {
425
- options [ i ] . label = options [ i ] . label + ' ' + priceUtils . formatPrice (
426
- optionPriceDiff ,
427
- this . options . priceFormat ,
428
- true ) ;
429
- }
436
+ }
437
+
438
+ if ( typeof allowedProductsAll [ 0 ] !== 'undefined' &&
439
+ typeof optionPrices [ allowedProductsAll [ 0 ] ] !== 'undefined' ) {
440
+ allowedProductsAllMinPrice = this . _getAllowedProductWithMinPrice ( allowedProductsAll ) ;
441
+ finalPrice = parseFloat ( optionPrices [ allowedProductsAllMinPrice ] . finalPrice . amount ) ;
442
+ }
443
+ }
444
+
445
+ for ( i = 0 ; i < options . length ; i ++ ) {
446
+ allowedProducts = prevConfig ? allowedProductsByOption [ i ] : options [ i ] . products . slice ( 0 ) ;
447
+ optionPriceDiff = 0 ;
448
+
449
+ if ( typeof allowedProducts [ 0 ] !== 'undefined' &&
450
+ typeof optionPrices [ allowedProducts [ 0 ] ] !== 'undefined' ) {
451
+ allowedProductMinPrice = this . _getAllowedProductWithMinPrice ( allowedProducts ) ;
452
+ optionFinalPrice = parseFloat ( optionPrices [ allowedProductMinPrice ] . finalPrice . amount ) ;
453
+ optionPriceDiff = optionFinalPrice - finalPrice ;
454
+ options [ i ] . label = options [ i ] . initialLabel ;
455
+
456
+ if ( optionPriceDiff !== 0 ) {
457
+ options [ i ] . label += ' ' + priceUtils . formatPrice (
458
+ optionPriceDiff ,
459
+ this . options . priceFormat ,
460
+ true
461
+ ) ;
430
462
}
431
463
}
432
464
0 commit comments