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 }