@@ -142,6 +142,17 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
142
142
if ( ! discountPerSku [ sku ] ) discountPerSku [ sku ] = 0
143
143
discountPerSku [ sku ] += discountValue
144
144
}
145
+ const pointDiscountToEachItem = ( discountValue , filteredItems ) => {
146
+ const itemsAmount = filteredItems . reduce (
147
+ ( amount , item ) => amount + ( ecomUtils . price ( item ) * ( item . quantity || 1 ) ) ,
148
+ 0
149
+ )
150
+ const discountMultiplier = discountValue / itemsAmount
151
+ filteredItems . forEach ( ( item ) => {
152
+ const discountPerItem = discountMultiplier * ( ecomUtils . price ( item ) * ( item . quantity || 1 ) )
153
+ return pointDiscountToSku ( discountPerItem , item . sku )
154
+ } )
155
+ }
145
156
146
157
const getFreebiesPreview = ( ) => {
147
158
if ( params . items && params . items . length ) {
@@ -441,8 +452,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
441
452
} )
442
453
} else {
443
454
const discountValue = addDiscount ( discount , `KIT-${ ( index + 1 ) } ` , kitDiscount . label )
444
- const discountPerItem = discountValue / kitItems . length
445
- kitItems . forEach ( ( item ) => pointDiscountToSku ( discountPerItem , item . sku ) )
455
+ pointDiscountToEachItem ( discountValue , kitItems )
446
456
}
447
457
discountedItemIds = discountedItemIds . concat ( kitItems . map ( item => item . product_id ) )
448
458
}
@@ -569,8 +579,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
569
579
const discountValue = addDiscount ( discountRule . discount , discountMatchEnum )
570
580
if ( discountValue ) {
571
581
if ( filteredItems ?. length ) {
572
- const discountPerItem = discountValue / filteredItems . length
573
- filteredItems . forEach ( ( item ) => pointDiscountToSku ( discountPerItem , item . sku ) )
582
+ pointDiscountToEachItem ( discountValue , filteredItems )
574
583
}
575
584
// add discount label and description if any
576
585
response . discount_rule . label = label
0 commit comments