From eb376eb4cf9cc6fe0ce2a6724e0df81b27bc7b87 Mon Sep 17 00:00:00 2001 From: Abdulrahman Date: Mon, 18 Aug 2025 15:50:25 +0300 Subject: [PATCH] fix(core-flows): handle missing variants and preserve zero unit_price in prepareLineItems (#13179) * fix: handle missing variants and preserve zero unit_price in prepareLineItems Fixed two issues in prepareLineItems: 1. unit_price value of 0 was previously treated as falsy and overwritten with the variant's price. Updated the check to only fallback when unitPrice is null or undefined. 2. When no variants array was provided, the code could throw due to non-null assertions. Now safely handles missing or empty variants. Additional adjustments: - Replaced `||` with `??` for array defaults to preserve valid empty arrays. - Kept changes minimal to avoid altering function signature. * Create sweet-wasps-build.md * Update add-line-items.ts * Update packages/core/core-flows/src/order/workflows/add-line-items.ts Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> * Update packages/core/core-flows/src/order/workflows/create-order.ts Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --------- Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --- .changeset/sweet-wasps-build.md | 5 +++++ .../core-flows/src/order/workflows/add-line-items.ts | 8 ++++---- .../core-flows/src/order/workflows/create-order.ts | 10 +++++----- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 .changeset/sweet-wasps-build.md diff --git a/.changeset/sweet-wasps-build.md b/.changeset/sweet-wasps-build.md new file mode 100644 index 0000000000..78681055f6 --- /dev/null +++ b/.changeset/sweet-wasps-build.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix: handle missing variants and preserve zero unit_price in prepareLineItems diff --git a/packages/core/core-flows/src/order/workflows/add-line-items.ts b/packages/core/core-flows/src/order/workflows/add-line-items.ts index 7898497c50..862a601740 100644 --- a/packages/core/core-flows/src/order/workflows/add-line-items.ts +++ b/packages/core/core-flows/src/order/workflows/add-line-items.ts @@ -31,7 +31,7 @@ import { productVariantsFields } from "../utils/fields" function prepareLineItems(data) { const items = (data.input.items ?? []).map((item) => { - const variant = data.variants?.find((v) => v.id === item.variant_id)! + const variant = data.variants?.find((v) => v.id === item.variant_id) const input: PrepareLineItemDataInput = { item, @@ -41,11 +41,11 @@ function prepareLineItems(data) { item.is_tax_inclusive ?? variant?.calculated_price?.is_calculated_price_tax_inclusive, isCustomPrice: isDefined(item?.unit_price), - taxLines: item.tax_lines || [], - adjustments: item.adjustments || [], + taxLines: item.tax_lines ?? [], + adjustments: item.adjustments ?? [], } - if (variant && !input.unitPrice) { + if (variant && !isDefined(input.unitPrice)) { input.unitPrice = variant.calculated_price?.calculated_amount } diff --git a/packages/core/core-flows/src/order/workflows/create-order.ts b/packages/core/core-flows/src/order/workflows/create-order.ts index e3c1810cb8..c57f776e15 100644 --- a/packages/core/core-flows/src/order/workflows/create-order.ts +++ b/packages/core/core-flows/src/order/workflows/create-order.ts @@ -33,21 +33,21 @@ import { updateOrderTaxLinesWorkflow } from "./update-tax-lines" function prepareLineItems(data) { const items = (data.input.items ?? []).map((item) => { - const variant = data.variants.find((v) => v.id === item.variant_id)! + const variant = data.variants?.find((v) => v.id === item.variant_id) const input: PrepareLineItemDataInput = { item, variant: variant, - unitPrice: item.unit_price ?? undefined, + unitPrice: item.unit_price, isTaxInclusive: item.is_tax_inclusive ?? variant?.calculated_price?.is_calculated_price_tax_inclusive, isCustomPrice: isDefined(item?.unit_price), - taxLines: item.tax_lines || [], - adjustments: item.adjustments || [], + taxLines: item.tax_lines ?? [], + adjustments: item.adjustments ?? [], } - if (variant && !input.unitPrice) { + if (variant && !isDefined(input.unitPrice)) { input.unitPrice = variant.calculated_price?.calculated_amount }