From 67b308c8eb52950991df34ac1c8d007a14cfaa1e Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 13 Mar 2025 18:09:46 +0100 Subject: [PATCH] fix(promotion): percentage value is accounted for in buyget promotions (#11799) what: - allows percentage value to be considered for buy get percentage FIXES https://github.com/medusajs/medusa/issues/11259 --- .changeset/friendly-comics-sort.md | 5 +++ .../promotion-module/compute-actions.spec.ts | 42 +++++++++---------- .../src/utils/compute-actions/buy-get.ts | 4 +- 3 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 .changeset/friendly-comics-sort.md diff --git a/.changeset/friendly-comics-sort.md b/.changeset/friendly-comics-sort.md new file mode 100644 index 0000000000..37eb0b8e2b --- /dev/null +++ b/.changeset/friendly-comics-sort.md @@ -0,0 +1,5 @@ +--- +"@medusajs/promotion": patch +--- + +fix(promotion): percentage value is accounted for in buyget promotions diff --git a/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts b/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts index 98b67a323b..70cb09f147 100644 --- a/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts +++ b/packages/modules/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts @@ -4443,9 +4443,9 @@ moduleIntegrationTestRunner({ }, ], application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 1000, + value: 100, allocation: "each", max_quantity: 1, apply_to_quantity: 1, @@ -4623,11 +4623,11 @@ moduleIntegrationTestRunner({ ], campaign_id: undefined, application_method: { - type: "fixed", + type: "percentage", target_type: "items", allocation: "each", max_quantity: 1, - value: 1000, + value: 100, apply_to_quantity: 4, buy_rules_min_quantity: 1, target_rules: [ @@ -4764,9 +4764,9 @@ moduleIntegrationTestRunner({ buyXGetXPromotion = await createDefaultPromotion(service, { type: PromotionType.BUYGET, application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 2000, + value: 100, allocation: "each", max_quantity: 2, apply_to_quantity: 2, @@ -4827,13 +4827,13 @@ moduleIntegrationTestRunner({ const buyXGetXPromotionBulk1 = await createDefaultPromotion( service, { - code: "BUY50GET1000", + code: "BUY50GET100", type: PromotionType.BUYGET, campaign_id: null, application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 20000, + value: 100, allocation: "each", max_quantity: 1000, apply_to_quantity: 1000, @@ -4863,9 +4863,9 @@ moduleIntegrationTestRunner({ type: PromotionType.BUYGET, campaign_id: null, application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 20000, + value: 20, allocation: "each", max_quantity: 20, apply_to_quantity: 20, @@ -4910,11 +4910,11 @@ moduleIntegrationTestRunner({ action: "addItemAdjustment", item_id: "item_cotton_tshirt", amount: 2500, - code: "BUY50GET1000", + code: "BUY50GET100", }, { action: "addItemAdjustment", - amount: 50, + amount: 10, code: "BUY10GET20", item_id: "item_cotton_tshirt", }, @@ -4925,13 +4925,13 @@ moduleIntegrationTestRunner({ const buyXGetXPromotionBulk1 = await createDefaultPromotion( service, { - code: "BUY50GET1000", + code: "BUY50GET100", type: PromotionType.BUYGET, campaign_id: null, application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 20000, + value: 100, allocation: "each", max_quantity: 1000, apply_to_quantity: 1000, @@ -4961,9 +4961,9 @@ moduleIntegrationTestRunner({ type: PromotionType.BUYGET, campaign_id: null, application_method: { - type: "fixed", + type: "percentage", target_type: "items", - value: 20000, + value: 20, allocation: "each", max_quantity: 20, apply_to_quantity: 20, @@ -5018,18 +5018,18 @@ moduleIntegrationTestRunner({ action: "addItemAdjustment", item_id: "item_cotton_tshirt2", amount: 1225, - code: "BUY50GET1000", + code: "BUY50GET100", }, { action: "addItemAdjustment", item_id: "item_cotton_tshirt", amount: 1275, - code: "BUY50GET1000", + code: "BUY50GET100", }, { action: "addItemAdjustment", item_id: "item_cotton_tshirt2", - amount: 50, + amount: 10, code: "BUY10GET20", }, ]) diff --git a/packages/modules/promotion/src/utils/compute-actions/buy-get.ts b/packages/modules/promotion/src/utils/compute-actions/buy-get.ts index caaae3295a..26c592eba3 100644 --- a/packages/modules/promotion/src/utils/compute-actions/buy-get.ts +++ b/packages/modules/promotion/src/utils/compute-actions/buy-get.ts @@ -200,10 +200,12 @@ export function getComputedActionsForBuyGet( const appliedPromoValue = methodIdPromoValueMap.get(item.id) ?? MathBN.convert(0) const multiplier = MathBN.min(targetItem.quantity, remainingQtyToApply) - const amount = MathBN.mult( + const applicableAmount = MathBN.mult( MathBN.div(item.subtotal, item.quantity), multiplier ) + const applicablePercentage = promotion.application_method?.value ?? 100 + const amount = MathBN.mult(applicableAmount, applicablePercentage).div(100) const newRemainingQtyToApply = MathBN.sub(remainingQtyToApply, multiplier)