@@ -123,6 +123,8 @@ define([
123123 if ( this . options . spConfig . inputsInitialized ) {
124124 this . _setValuesByAttribute ( ) ;
125125 }
126+
127+ this . _setInitialOptionsLabels ( ) ;
126128 } ,
127129
128130 /**
@@ -158,6 +160,18 @@ define([
158160 } , this ) ) ;
159161 } ,
160162
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+
161175 /**
162176 * Set up .on('change') events for each option element to configure the option.
163177 * @private
@@ -371,6 +385,8 @@ define([
371385 prevConfig ,
372386 index = 1 ,
373387 allowedProducts ,
388+ allowedProductsByOption ,
389+ allowedProductsAll ,
374390 i ,
375391 j ,
376392 finalPrice = parseFloat ( this . options . spConfig . prices . finalPrice . amount ) ,
@@ -379,7 +395,8 @@ define([
379395 optionPrices = this . options . spConfig . optionPrices ,
380396 allowedOptions = [ ] ,
381397 indexKey ,
382- allowedProductMinPrice ;
398+ allowedProductMinPrice ,
399+ allowedProductsAllMinPrice ;
383400
384401 this . _clearSelect ( element ) ;
385402 element . options [ 0 ] = new Option ( '' , '' ) ;
@@ -398,35 +415,50 @@ define([
398415 }
399416 }
400417
401- for ( i = 0 ; i < options . length ; i ++ ) {
402- allowedProducts = [ ] ;
403- optionPriceDiff = 0 ;
418+ if ( prevConfig ) {
419+ allowedProductsByOption = { } ;
420+ allowedProductsAll = [ ] ;
404421
405- /* eslint-disable max-depth */
406- if ( prevConfig ) {
422+ for ( i = 0 ; i < options . length ; i ++ ) {
423+ /* eslint-disable max-depth */
407424 for ( j = 0 ; j < options [ i ] . products . length ; j ++ ) {
408425 // prevConfig.config can be undefined
409426 if ( prevConfig . config &&
410427 prevConfig . config . allowedProducts &&
411428 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 ] ) ;
413434 }
414435 }
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+ ) ;
430462 }
431463 }
432464
0 commit comments