Skip to content

fix(core-flows): updating tax lines when draft order shipping is removed #12919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-masks-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

fix(core-flows): updating tax lines when draft order shipping is removed
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ medusaIntegrationTestRunner({
let stockLocation: HttpTypes.AdminStockLocation
let testDraftOrder: HttpTypes.AdminDraftOrder
let shippingOption: HttpTypes.AdminShippingOption
let shippingOptionHeavy: HttpTypes.AdminShippingOption

beforeEach(async () => {
const container = getContainer()
Expand Down Expand Up @@ -55,6 +56,14 @@ medusaIntegrationTestRunner({
)
).data.shipping_profile

const shippingProfileHeavy = (
await api.post(
`/admin/shipping-profiles`,
{ name: "test shipping profile heavy", type: "heavy" },
adminHeaders
)
).data.shipping_profile

const fulfillmentSets = (
await api.post(
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
Expand Down Expand Up @@ -103,7 +112,28 @@ medusaIntegrationTestRunner({
description: "Test description",
code: "test-code",
},
prices: [{ currency_code: "usd", amount: 1000 }],
prices: [{ currency_code: "usd", amount: 5 }],
rules: [],
},
adminHeaders
)
).data.shipping_option

shippingOptionHeavy = (
await api.post(
`/admin/shipping-options`,
{
name: `Test shipping option ${fulfillmentSet.id}`,
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfileHeavy.id,
provider_id: "manual_test-provider",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
prices: [{ currency_code: "usd", amount: 10 }],
rules: [],
},
adminHeaders
Expand All @@ -117,6 +147,13 @@ medusaIntegrationTestRunner({
email: "[email protected]",
region_id: region.id,
sales_channel_id: salesChannel.id,
shipping_address: {
address_1: "123 Main St",
city: "Anytown",
country_code: "US",
postal_code: "12345",
first_name: "John",
},
},
adminHeaders
)
Expand Down Expand Up @@ -610,6 +647,152 @@ medusaIntegrationTestRunner({

expect(order.shipping_methods.length).toBe(0)
})

it("should ensure that the shipping method is removed from the order and tax lines are updated with multiple shipping methods", async () => {
/**
* Add Heavy SO
*/

edit = (
await api.post(
`/admin/draft-orders/${testDraftOrder.id}/edit`,
{},
adminHeaders
)
).data.draft_order_preview

await api.post(
`/admin/draft-orders/${testDraftOrder.id}/edit/shipping-methods`,
{
shipping_option_id: shippingOptionHeavy.id,
},
adminHeaders
)

edit = (
await api.post(
`/admin/draft-orders/${testDraftOrder.id}/edit/confirm`,
{},
adminHeaders
)
).data.draft_order_preview

/**
* Tax rate -> 2%
*
* One product -> 10$
* Shipping method 1 -> 5$
* Shipping method 2 -> 10$
*/

expect(edit).toEqual(
expect.objectContaining({
total: 25.5,
subtotal: 25,
tax_total: 0.5,

items: [
expect.objectContaining({
subtotal: 10,
total: 10.2,
tax_total: 0.2,
tax_lines: [
expect.objectContaining({
rate: 2,
}),
],
}),
],
shipping_methods: expect.arrayContaining([
expect.objectContaining({
shipping_option_id: shippingOption.id,
amount: 5,
subtotal: 5,
total: 5.1,
tax_total: 0.1,
}),
expect.objectContaining({
shipping_option_id: shippingOptionHeavy.id,
amount: 10,
subtotal: 10,
total: 10.2,
tax_total: 0.2,
}),
]),
})
)

/**
* Remove Heavy shipping method
*/

edit = (
await api.post(
`/admin/draft-orders/${testDraftOrder.id}/edit`,
{},
adminHeaders
)
).data.draft_order_preview

const response = await api.delete(
`/admin/draft-orders/${
testDraftOrder.id
}/edit/shipping-methods/method/${
edit.shipping_methods.find(
(sm) => sm.shipping_option_id === shippingOptionHeavy.id
).id
}`,
adminHeaders
)

expect(response.status).toBe(200)
expect(response.data.draft_order_preview.shipping_methods.length).toBe(
1
)

await api.post(
`/admin/draft-orders/${testDraftOrder.id}/edit/confirm`,
{},
adminHeaders
)

const order = (
await api.get(
`/admin/draft-orders/${testDraftOrder.id}?fields=+total,+subtotal,+tax_total,+items.subtotal,+items.total,+items.tax_total,+shipping_methods.amount,+shipping_methods.subtotal,+shipping_methods.total,+shipping_methods.tax_total`,
adminHeaders
)
).data.draft_order

expect(order).toEqual(
expect.objectContaining({
total: 15.3,
subtotal: 15,
tax_total: 0.3,

items: [
expect.objectContaining({
subtotal: 10,
total: 10.2,
tax_total: 0.2,
tax_lines: [
expect.objectContaining({
rate: 2,
}),
],
}),
],
shipping_methods: expect.arrayContaining([
expect.objectContaining({
shipping_option_id: shippingOption.id,
amount: 5,
subtotal: 5,
total: 5.1,
tax_total: 0.1,
}),
]),
})
)
})
})
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { useRemoteQueryStep } from "../../common"
import {
createOrderChangeActionsWorkflow,
previewOrderChangeStep,
updateOrderTaxLinesWorkflow,
} from "../../order"
import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-change"
import { draftOrderFieldsForRefreshSteps } from "../utils/fields"
Expand Down Expand Up @@ -87,12 +86,6 @@ export const removeDraftOrderShippingMethodWorkflow = createWorkflow(

validateDraftOrderChangeStep({ order, orderChange })

updateOrderTaxLinesWorkflow.runAsStep({
input: {
order_id: order.id,
},
})

const appliedPromoCodes: string[] = transform(
order,
(order) => order.promotions?.map((promotion) => promotion.code) ?? []
Expand Down