feat(): added percentage calculations for all cases (#6300)
what: - adds missing percentage calculations for items and shipping methods
This commit is contained in:
@@ -46,7 +46,10 @@ export function getComputedActionsForBuyGet(
|
||||
const validItemsForTargetRules = itemsContext
|
||||
.filter((item) => areRulesValidForContext(targetRules, item))
|
||||
.sort((a, b) => {
|
||||
return b.unit_price - a.unit_price
|
||||
const aPrice = a.subtotal / a.quantity
|
||||
const bPrice = b.subtotal / b.quantity
|
||||
|
||||
return bPrice - aPrice
|
||||
})
|
||||
|
||||
let remainingQtyToApply = applyToQuantity
|
||||
@@ -54,7 +57,7 @@ export function getComputedActionsForBuyGet(
|
||||
for (const method of validItemsForTargetRules) {
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
const multiplier = Math.min(method.quantity, remainingQtyToApply)
|
||||
const amount = method.unit_price * multiplier
|
||||
const amount = (method.subtotal / method.quantity) * multiplier
|
||||
const newRemainingQtyToApply = remainingQtyToApply - multiplier
|
||||
|
||||
if (newRemainingQtyToApply < 0 || amount <= 0) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
import {
|
||||
ApplicationMethodAllocation,
|
||||
ApplicationMethodTargetType,
|
||||
ApplicationMethodType,
|
||||
ComputedActions,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
@@ -67,10 +68,15 @@ export function applyPromotionToItems(
|
||||
method.quantity,
|
||||
applicationMethod?.max_quantity!
|
||||
)
|
||||
const promotionValue =
|
||||
parseFloat(applicationMethod!.value!) * quantityMultiplier
|
||||
const applicableTotal =
|
||||
method.unit_price * quantityMultiplier - appliedPromoValue
|
||||
const totalItemValue =
|
||||
(method.subtotal / method.quantity) * quantityMultiplier
|
||||
let promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const applicableTotal = totalItemValue - appliedPromoValue
|
||||
|
||||
if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
|
||||
promotionValue = (promotionValue / 100) * applicableTotal
|
||||
}
|
||||
|
||||
const amount = Math.min(promotionValue, applicableTotal)
|
||||
|
||||
if (amount <= 0) {
|
||||
@@ -106,18 +112,32 @@ export function applyPromotionToItems(
|
||||
) {
|
||||
const totalApplicableValue = items!.reduce((acc, method) => {
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
return acc + method.unit_price * method.quantity - appliedPromoValue
|
||||
return (
|
||||
acc +
|
||||
(method.subtotal / method.quantity) * method.quantity -
|
||||
appliedPromoValue
|
||||
)
|
||||
}, 0)
|
||||
|
||||
for (const method of items!) {
|
||||
const promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
const promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const applicableTotal =
|
||||
method.unit_price * method.quantity - appliedPromoValue
|
||||
(method.subtotal / method.quantity) * method.quantity -
|
||||
appliedPromoValue
|
||||
|
||||
if (applicableTotal <= 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO: should we worry about precision here?
|
||||
const applicablePromotionValue =
|
||||
let applicablePromotionValue =
|
||||
(applicableTotal / totalApplicableValue) * promotionValue
|
||||
|
||||
if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
|
||||
applicablePromotionValue = (promotionValue / 100) * applicableTotal
|
||||
}
|
||||
|
||||
const amount = Math.min(applicablePromotionValue, applicableTotal)
|
||||
|
||||
if (amount <= 0) {
|
||||
@@ -135,6 +155,8 @@ export function applyPromotionToItems(
|
||||
continue
|
||||
}
|
||||
|
||||
methodIdPromoValueMap.set(method.id, appliedPromoValue + amount)
|
||||
|
||||
computedActions.push({
|
||||
action: ComputedActions.ADD_ITEM_ADJUSTMENT,
|
||||
item_id: method.id,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
ApplicationMethodAllocation,
|
||||
ApplicationMethodTargetType,
|
||||
ApplicationMethodType,
|
||||
ComputedActions,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
@@ -55,8 +56,13 @@ export function applyPromotionToShippingMethods(
|
||||
if (allocation === ApplicationMethodAllocation.EACH) {
|
||||
for (const method of shippingMethods!) {
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
const promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const applicableTotal = method.unit_price - appliedPromoValue
|
||||
let promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const applicableTotal = method.subtotal - appliedPromoValue
|
||||
|
||||
if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
|
||||
promotionValue = (promotionValue / 100) * applicableTotal
|
||||
}
|
||||
|
||||
const amount = Math.min(promotionValue, applicableTotal)
|
||||
|
||||
if (amount <= 0) {
|
||||
@@ -89,7 +95,7 @@ export function applyPromotionToShippingMethods(
|
||||
const totalApplicableValue = shippingMethods!.reduce((acc, method) => {
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
|
||||
return acc + method.unit_price - appliedPromoValue
|
||||
return acc + method.subtotal - appliedPromoValue
|
||||
}, 0)
|
||||
|
||||
if (totalApplicableValue <= 0) {
|
||||
@@ -98,14 +104,19 @@ export function applyPromotionToShippingMethods(
|
||||
|
||||
for (const method of shippingMethods!) {
|
||||
const promotionValue = parseFloat(applicationMethod!.value!)
|
||||
const applicableTotal = method.unit_price
|
||||
const applicableTotal = method.subtotal
|
||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) || 0
|
||||
|
||||
// TODO: should we worry about precision here?
|
||||
const applicablePromotionValue =
|
||||
let applicablePromotionValue =
|
||||
(applicableTotal / totalApplicableValue) * promotionValue -
|
||||
appliedPromoValue
|
||||
|
||||
if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
|
||||
applicablePromotionValue =
|
||||
(promotionValue / 100) * (applicableTotal - appliedPromoValue)
|
||||
}
|
||||
|
||||
const amount = Math.min(applicablePromotionValue, applicableTotal)
|
||||
|
||||
if (amount <= 0) {
|
||||
|
||||
Reference in New Issue
Block a user