fix(promotion, types): non discountable items check (#12644)

* fix(promotions): check if item is discountable

* fix: return earl yonly if non discountable

* fix: update test

* chore: add integration test
This commit is contained in:
Frane Polić
2025-06-12 10:23:06 +02:00
committed by GitHub
parent 6be5750fe8
commit bd6d9777c5
5 changed files with 175 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/promotion": patch
"@medusajs/types": patch
---
fix(promotion, types): non discountable items check

View File

@@ -2621,6 +2621,96 @@ medusaIntegrationTestRunner({
)
})
it("should only apply promotion on discountable items", async () => {
const notDiscountableProduct = (
await api.post(
"/admin/products",
{
title: "Medusa T-Shirt not discountable",
handle: "t-shirt-not-discountable",
discountable: false,
options: [
{
title: "Size",
values: ["S"],
},
],
variants: [
{
title: "S",
sku: "s-shirt",
options: {
Size: "S",
},
manage_inventory: false,
prices: [
{
amount: 1000,
currency_code: "usd",
},
],
},
],
shipping_profile_id: shippingProfile.id,
},
adminHeaders
)
).data.product
const cartData = {
currency_code: "usd",
sales_channel_id: salesChannel.id,
region_id: region.id,
shipping_address: shippingAddressData,
items: [
{ variant_id: product.variants[0].id, quantity: 1 },
{
variant_id: notDiscountableProduct.variants[0].id,
quantity: 1,
},
],
promo_codes: [promotion.code],
}
const cart = (
await api.post(
`/store/carts?fields=+items.is_discountable,+items.total,+items.discount_total`,
cartData,
storeHeaders
)
).data.cart
expect(cart).toEqual(
expect.objectContaining({
discount_subtotal: 100,
items: expect.arrayContaining([
expect.objectContaining({
variant_id: product.variants[0].id,
is_discountable: true,
unit_price: 1500,
total: 1395,
discount_total: 100,
adjustments: [
expect.objectContaining({
promotion_id: promotion.id,
amount: 100,
}),
],
}),
expect.objectContaining({
variant_id: notDiscountableProduct.variants[0].id,
is_discountable: false,
total: 1000,
unit_price: 1000,
discount_total: 0,
adjustments: [],
}),
]),
})
)
})
it("should remove promotion adjustments when promotion is deleted", async () => {
let cartBeforeRemovingPromotion = (
await api.get(`/store/carts/${cart.id}`, storeHeaders)

View File

@@ -162,6 +162,71 @@ medusaIntegrationTestRunner({
)
})
it("should add line item adjustments only for discountable items", async () => {
const createdPromotion =
await promotionModuleService.createPromotions({
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: 1000,
apply_to_quantity: 1,
currency_code: "usd",
},
})
const cart = await cartModuleService.createCarts({
currency_code: "usd",
items: [
{
id: "item-1",
unit_price: 2000,
quantity: 1,
title: "Test item",
product_id: "prod_mat",
} as any,
{
id: "item-2",
unit_price: 1000,
quantity: 1,
title: "Test item",
product_id: "prod_tshirt",
is_discountable: false,
} as any,
],
})
const created = await api.post(
`/store/carts/${cart.id}/promotions`,
{ promo_codes: [createdPromotion.code] },
storeHeaders
)
expect(created.status).toEqual(200)
expect(created.data.cart).toEqual(
expect.objectContaining({
id: expect.any(String),
items: expect.arrayContaining([
expect.objectContaining({
id: "item-1",
adjustments: expect.arrayContaining([
expect.objectContaining({
code: createdPromotion.code,
amount: 1000,
}),
]),
}),
expect.objectContaining({
adjustments: [],
}),
]),
})
)
})
it("should add shipping method adjustments to a cart based on promotions", async () => {
const [appliedPromotion] =
await promotionModuleService.createPromotions([

View File

@@ -180,6 +180,11 @@ export interface ComputeActionItemLine extends Record<string, unknown> {
*/
subtotal: BigNumberInput
/**
* Whether the line item is discountable.
*/
is_discountable: boolean
/**
* The adjustments applied before on the line item.
*/

View File

@@ -201,7 +201,15 @@ function getValidItemsForPromotion(
}
return items.filter((item) => {
if (!item || !("subtotal" in item) || MathBN.lte(item.subtotal, 0)) {
if (!item) {
return false
}
if ("is_discountable" in item && !item.is_discountable) {
return false
}
if (!("subtotal" in item) || MathBN.lte(item.subtotal, 0)) {
return false
}