Skip to content

Commit aa59676

Browse files
committed
fix: better calculating discount per item for extra discount description
1 parent 5a40927 commit aa59676

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

functions/routes/discount-per-item.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ exports.post = async ({ appSdk }, req, res) => {
2020
return res.send({ items })
2121
}
2222
if (!extraDiscount.app?.description) {
23-
const discountMultiplier = 1 - extraDiscount.value / amount.subtotal
23+
const priceMultiplier = 1 - extraDiscount.value / amount.subtotal
2424
return res.send({
2525
items: items.map((item) => ({
2626
...item,
27-
final_price: parseInt(ecomUtils.price(item) * discountMultiplier * 100, 10) / 100
27+
final_price: parseInt(ecomUtils.price(item) * priceMultiplier * 100, 10) / 100
2828
}))
2929
})
3030
}

functions/routes/ecom/modules/apply-discount.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
142142
if (!discountPerSku[sku]) discountPerSku[sku] = 0
143143
discountPerSku[sku] += discountValue
144144
}
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+
}
145156

146157
const getFreebiesPreview = () => {
147158
if (params.items && params.items.length) {
@@ -441,8 +452,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
441452
})
442453
} else {
443454
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)
446456
}
447457
discountedItemIds = discountedItemIds.concat(kitItems.map(item => item.product_id))
448458
}
@@ -569,8 +579,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
569579
const discountValue = addDiscount(discountRule.discount, discountMatchEnum)
570580
if (discountValue) {
571581
if (filteredItems?.length) {
572-
const discountPerItem = discountValue / filteredItems.length
573-
filteredItems.forEach((item) => pointDiscountToSku(discountPerItem, item.sku))
582+
pointDiscountToEachItem(discountValue, filteredItems)
574583
}
575584
// add discount label and description if any
576585
response.discount_rule.label = label

0 commit comments

Comments
 (0)