From f0bc2a11f304f716783d03ba560880b8428fa470 Mon Sep 17 00:00:00 2001 From: Abhishek010 Date: Fri, 3 Jul 2026 23:30:45 +0530 Subject: [PATCH 1/3] fix(cart): prevent crash when variant price is missing --- .../src/cart/workflows/get-variants-and-items-with-prices.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/core-flows/src/cart/workflows/get-variants-and-items-with-prices.ts b/packages/core/core-flows/src/cart/workflows/get-variants-and-items-with-prices.ts index 149a629d5d91e..520dc81910a14 100644 --- a/packages/core/core-flows/src/cart/workflows/get-variants-and-items-with-prices.ts +++ b/packages/core/core-flows/src/cart/workflows/get-variants-and-items-with-prices.ts @@ -135,7 +135,7 @@ export const prepareVariantsAndItemsWithPricesStep = createStep( isCustomPrice: isCustomPrice, } - if (variant && !isCustomPrice) { + if (variant && !isCustomPrice && calculatedPriceSet) { input.unitPrice = calculatedPriceSet.calculated_amount input.isTaxInclusive = calculatedPriceSet.is_calculated_price_tax_inclusive From 20f472cfd48b8442da60c84215ec0c5891882424 Mon Sep 17 00:00:00 2001 From: Abhishek010 Date: Fri, 3 Jul 2026 23:34:46 +0530 Subject: [PATCH 2/3] fix(cart): prevent crash when variant price is missing --- .changeset/fix-calculatedPriceSet-guard.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-calculatedPriceSet-guard.md diff --git a/.changeset/fix-calculatedPriceSet-guard.md b/.changeset/fix-calculatedPriceSet-guard.md new file mode 100644 index 0000000000000..a103c1838446d --- /dev/null +++ b/.changeset/fix-calculatedPriceSet-guard.md @@ -0,0 +1,5 @@ +--- +"@medusajs/query": patch +--- + +Refactor query logic in a dedicated package From a39c8ecef377924f4eef3f20d9bfd27b2768f398 Mon Sep 17 00:00:00 2001 From: Abhishek010 Date: Sat, 4 Jul 2026 00:04:59 +0530 Subject: [PATCH 3/3] added tests and fixes --- .changeset/fix-calculatedPriceSet-guard.md | 4 +- ...get-variants-and-items-with-prices.spec.ts | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 packages/core/core-flows/src/cart/workflows/__tests__/get-variants-and-items-with-prices.spec.ts diff --git a/.changeset/fix-calculatedPriceSet-guard.md b/.changeset/fix-calculatedPriceSet-guard.md index a103c1838446d..5a2c9e241b508 100644 --- a/.changeset/fix-calculatedPriceSet-guard.md +++ b/.changeset/fix-calculatedPriceSet-guard.md @@ -1,5 +1,5 @@ --- -"@medusajs/query": patch +"@medusajs/core-flows": patch --- -Refactor query logic in a dedicated package +Return an invalid data error when adding a cart line item for a variant without a price diff --git a/packages/core/core-flows/src/cart/workflows/__tests__/get-variants-and-items-with-prices.spec.ts b/packages/core/core-flows/src/cart/workflows/__tests__/get-variants-and-items-with-prices.spec.ts new file mode 100644 index 0000000000000..263c65f11a9fa --- /dev/null +++ b/packages/core/core-flows/src/cart/workflows/__tests__/get-variants-and-items-with-prices.spec.ts @@ -0,0 +1,41 @@ +import { createContainer } from "@medusajs/framework/awilix" +import { MedusaError, ProductStatus } from "@medusajs/framework/utils" +import { + createWorkflow, + WorkflowResponse, +} from "@medusajs/framework/workflows-sdk" +import { prepareVariantsAndItemsWithPricesStep } from "../get-variants-and-items-with-prices" + +describe("prepareVariantsAndItemsWithPricesStep", () => { + it("throws an invalid data error when a variant does not have a price", async () => { + const workflow = createWorkflow( + "test-prepare-variant-without-price", + () => { + const result = prepareVariantsAndItemsWithPricesStep({ + cart: { id: "cart_1" }, + items: [{ variant_id: "variant_1", quantity: 1 }], + variantsData: [ + { + id: "variant_1", + product: { + id: "product_1", + status: ProductStatus.PUBLISHED, + }, + inventory_items: [], + }, + ], + calculatedPriceSets: {}, + }) + + return new WorkflowResponse(result) + } + ) + + const execution = workflow(createContainer()).run() + + await expect(execution).rejects.toMatchObject({ + type: MedusaError.Types.INVALID_DATA, + message: "Variants with IDs variant_1 do not have a price", + }) + }) +})