diff --git a/.changeset/fix-calculatedPriceSet-guard.md b/.changeset/fix-calculatedPriceSet-guard.md new file mode 100644 index 0000000000000..5a2c9e241b508 --- /dev/null +++ b/.changeset/fix-calculatedPriceSet-guard.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +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", + }) + }) +}) 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