Skip to content

Commit 3ae4162

Browse files
authored
fix(core-flows): force cart refresh on updatePromotionsWorkflow when refreshing items (#14569)
1 parent a29e16a commit 3ae4162

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

.changeset/clear-swans-stick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
fix(core-flows): force cart refresh on updatePromotionsWorkflow when refreshing items

integration-tests/http/__tests__/shipping-option/store/shipping-option-calculated.spec.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,109 @@ medusaIntegrationTestRunner({
401401
})
402402
)
403403
})
404+
405+
it("should recompute shipping promotion adjustments after shipping price changes due to item update", async () => {
406+
// Create an automatic promotion targeting shipping_methods
407+
// 100% off shipping (percentage) when applied
408+
await api.post(
409+
`/admin/promotions`,
410+
{
411+
code: "FREE_SHIPPING",
412+
type: "standard",
413+
status: "active",
414+
is_automatic: true,
415+
application_method: {
416+
type: "percentage",
417+
target_type: "shipping_methods",
418+
allocation: "each",
419+
value: 100,
420+
max_quantity: 1,
421+
currency_code: "usd",
422+
},
423+
},
424+
adminHeaders
425+
)
426+
427+
cart = (
428+
await api.post(
429+
`/store/carts`,
430+
{
431+
region_id: region.id,
432+
sales_channel_id: salesChannel.id,
433+
currency_code: "usd",
434+
435+
items: [
436+
{
437+
variant_id: product.variants[0].id,
438+
quantity: 2,
439+
},
440+
],
441+
},
442+
storeHeaders
443+
)
444+
).data.cart
445+
446+
// Add calculated shipping method to cart
447+
// With 2 items (quantity 2), price = 2 * 1.5 = 3
448+
let response = await api.post(
449+
`/store/carts/${cart.id}/shipping-methods`,
450+
{
451+
option_id: shippingOptionCalculated.id,
452+
data: { pin_id: "test" },
453+
},
454+
storeHeaders
455+
)
456+
457+
expect(response.data.cart).toEqual(
458+
expect.objectContaining({
459+
id: cart.id,
460+
shipping_methods: expect.arrayContaining([
461+
expect.objectContaining({
462+
shipping_option_id: shippingOptionCalculated.id,
463+
amount: 3,
464+
adjustments: expect.arrayContaining([
465+
expect.objectContaining({
466+
code: "FREE_SHIPPING",
467+
amount: 3,
468+
}),
469+
]),
470+
}),
471+
]),
472+
shipping_total: 0,
473+
})
474+
)
475+
476+
// Now add more items to the cart, which changes item quantity to 3
477+
// New shipping price should be 3 * 1.5 = 4.5
478+
// The promotion should adjust to 4.5 (100% off the NEW shipping amount)
479+
response = await api.post(
480+
`/store/carts/${cart.id}/line-items`,
481+
{
482+
variant_id: product.variants[0].id,
483+
quantity: 1,
484+
},
485+
storeHeaders
486+
)
487+
488+
expect(response.data.cart).toEqual(
489+
expect.objectContaining({
490+
id: cart.id,
491+
shipping_methods: expect.arrayContaining([
492+
expect.objectContaining({
493+
shipping_option_id: shippingOptionCalculated.id,
494+
amount: 4.5,
495+
adjustments: expect.arrayContaining([
496+
expect.objectContaining({
497+
code: "FREE_SHIPPING",
498+
amount: 4.5, // Must match the NEW shipping amount, not the old one (3)
499+
}),
500+
]),
501+
}),
502+
]),
503+
shipping_total: 0, // Still fully discounted
504+
})
505+
)
506+
})
404507
})
405508
})
406509
},

packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ export const refreshCartItemsWorkflow = createWorkflow(
237237
updateCartPromotionsWorkflow.runAsStep({
238238
input: {
239239
cart_id: input.cart_id,
240-
cart: refetchedCart, // Pass cart to avoid refetch in updateCartPromotionsWorkflow
241240
promo_codes: cartPromoCodes,
242241
action: PromotionActions.REPLACE,
243242
force_refresh_payment_collection: false,

0 commit comments

Comments
 (0)