fix(types,utils,promotion): Move from total to original_total to resolve edge case for adjustments calculation (#13106)

* Move from total to original_total to resolve edge case in adjustment calculation

* Added changeset

* Added test case for correction

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
scherddelandOli Juhl authored and GitHub committed 2025-08-01 12:52:04 +02:00
1 parent b37a87c355
commit 9766570827
5 files changed
+156 -9

No files matched your search

@@ -188,7 +188,7 @@ export interface ComputeActionItemLine extends Record<string, unknown> {
/**
* The total of the line item.
*/
total: BigNumberInput
original_total: BigNumberInput
/**
* Whether the line item is discountable.
@@ -218,7 +218,7 @@ export interface ComputeActionShippingLine extends Record<string, unknown> {
/**
* The total of the shipping method.
*/
total: BigNumberInput
original_total: BigNumberInput
/**
* The adjustments applied before on the shipping method.
@@ -53,8 +53,8 @@ function getLineItemSubtotal(lineItem) {
return MathBN.div(lineItem.subtotal, lineItem.quantity)
}
function getLineItemTotal(lineItem) {
return MathBN.div(lineItem.total, lineItem.quantity)
function getLineItemOriginalTotal(lineItem) {
return MathBN.div(lineItem.original_total, lineItem.quantity)
}
export function calculateAdjustmentAmountFromPromotion(
@@ -95,7 +95,7 @@ export function calculateAdjustmentAmountFromPromotion(
const lineItemAmount = MathBN.mult(
promotion.is_tax_inclusive
? getLineItemTotal(lineItem)
? getLineItemOriginalTotal(lineItem)
: getLineItemSubtotal(lineItem),
quantity
)
@@ -134,11 +134,11 @@ export function calculateAdjustmentAmountFromPromotion(
*/
const remainingItemAmount = MathBN.sub(
promotion.is_tax_inclusive ? lineItem.total : lineItem.subtotal,
promotion.is_tax_inclusive ? lineItem.original_total : lineItem.subtotal,
promotion.applied_value
)
const itemAmount = MathBN.div(
promotion.is_tax_inclusive ? lineItem.total : lineItem.subtotal,
promotion.is_tax_inclusive ? lineItem.original_total : lineItem.subtotal,
lineItem.quantity
)
const maximumPromotionAmount = MathBN.mult(
@@ -110,7 +110,7 @@ function applyPromotionToItems(
MathBN.sub(
MathBN.add(
acc,
promotion.is_tax_inclusive ? item.total : item.subtotal
promotion.is_tax_inclusive ? item.original_total : item.subtotal
),
appliedPromotionsMap.get(item.id) ?? 0
),
@@ -124,7 +124,10 @@ function applyPromotionToItems(
for (const item of applicableItems) {
if (
MathBN.lte(promotion.is_tax_inclusive ? item.total : item.subtotal, 0)
MathBN.lte(
promotion.is_tax_inclusive ? item.original_total : item.subtotal,
0
)
) {
continue
}