Skip to content

Commit

Permalink
fix: better calculating discount per item for extra discount description
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Aug 9, 2024
1 parent 5a40927 commit aa59676
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions functions/routes/discount-per-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ exports.post = async ({ appSdk }, req, res) => {
return res.send({ items })
}
if (!extraDiscount.app?.description) {
const discountMultiplier = 1 - extraDiscount.value / amount.subtotal
const priceMultiplier = 1 - extraDiscount.value / amount.subtotal
return res.send({
items: items.map((item) => ({
...item,
final_price: parseInt(ecomUtils.price(item) * discountMultiplier * 100, 10) / 100
final_price: parseInt(ecomUtils.price(item) * priceMultiplier * 100, 10) / 100
}))
})
}
Expand Down
17 changes: 13 additions & 4 deletions functions/routes/ecom/modules/apply-discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
if (!discountPerSku[sku]) discountPerSku[sku] = 0
discountPerSku[sku] += discountValue
}
const pointDiscountToEachItem = (discountValue, filteredItems) => {
const itemsAmount = filteredItems.reduce(
(amount, item) => amount + (ecomUtils.price(item) * (item.quantity || 1)),
0
)
const discountMultiplier = discountValue / itemsAmount
filteredItems.forEach((item) => {
const discountPerItem = discountMultiplier * (ecomUtils.price(item) * (item.quantity || 1))
return pointDiscountToSku(discountPerItem, item.sku)
})
}

const getFreebiesPreview = () => {
if (params.items && params.items.length) {
Expand Down Expand Up @@ -441,8 +452,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
})
} else {
const discountValue = addDiscount(discount, `KIT-${(index + 1)}`, kitDiscount.label)
const discountPerItem = discountValue / kitItems.length
kitItems.forEach((item) => pointDiscountToSku(discountPerItem, item.sku))
pointDiscountToEachItem(discountValue, kitItems)
}
discountedItemIds = discountedItemIds.concat(kitItems.map(item => item.product_id))
}
Expand Down Expand Up @@ -569,8 +579,7 @@ ${discountedSkus.map((sku) => `\n${sku}: ${discountPerSku[sku].toFixed(2)}`)}
const discountValue = addDiscount(discountRule.discount, discountMatchEnum)
if (discountValue) {
if (filteredItems?.length) {
const discountPerItem = discountValue / filteredItems.length
filteredItems.forEach((item) => pointDiscountToSku(discountPerItem, item.sku))
pointDiscountToEachItem(discountValue, filteredItems)
}
// add discount label and description if any
response.discount_rule.label = label
Expand Down

0 comments on commit aa59676

Please sign in to comment.