diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts index 72fd0f098e..c737657f4a 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts @@ -328,6 +328,69 @@ moduleIntegrationTestRunner({ }), ]) }) + + it("should correctly associate variants with own product options", async () => { + jest.clearAllMocks() + const productThree = await service.createProducts({ + id: "product-3", + title: "product 3", + status: ProductStatus.PUBLISHED, + options: [ + { + title: "size", + values: ["large", "small"], + }, + { + title: "color", + values: ["red", "blue"], + }, + ], + } as CreateProductDTO) + + const data: CreateProductVariantDTO[] = [ + { + title: "new variant", + product_id: productOne.id, + options: { size: "small" }, + }, + { + title: "new variant", + product_id: productThree.id, + options: { size: "small" }, + }, + ] + + const variants = await service.createProductVariants(data) + + expect(variants).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + title: "new variant", + product_id: productOne.id, + options: expect.arrayContaining([ + expect.objectContaining({ + id: productOne.options + .find((o) => o.title === "size") + ?.values?.find((v) => v.value === "small")?.id, + value: "small", + }), + ]), + }), + expect.objectContaining({ + title: "new variant", + product_id: productThree.id, + options: expect.arrayContaining([ + expect.objectContaining({ + id: productThree.options + .find((o) => o.title === "size") + ?.values?.find((v) => v.value === "small")?.id, + value: "small", + }), + ]), + }), + ]) + ) + }) }) describe("softDelete variant", () => { diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index e32944fb6b..b3029cf400 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -1710,7 +1710,10 @@ export default class ProductModuleService const variantsWithOptions = variants.map((variant: any) => { const variantOptions = Object.entries(variant.options ?? {}).map( ([key, val]) => { - const option = options.find((o) => o.title === key) + const option = options.find( + (o) => o.title === key && o.product_id === variant.product_id + ) + const optionValue = option?.values?.find( (v: any) => (v.value?.value ?? v.value) === val )