From 9b447f99ff25812ad07fe3b49656fd099df69e42 Mon Sep 17 00:00:00 2001 From: mortenengel <38915779+mortenengel@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:12:05 +0100 Subject: [PATCH] fix(medusa): Product option values in CSV Export (#5781) * https://github.com/medusajs/medusa/issues/5712 * fix-5712 added a comment * fix/5712 - changing to context object * Create yellow-games-reflect.md --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --- .changeset/yellow-games-reflect.md | 5 +++++ .../src/strategies/batch-jobs/product/export.ts | 12 +++++++++--- .../src/strategies/batch-jobs/product/types/index.ts | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 .changeset/yellow-games-reflect.md diff --git a/.changeset/yellow-games-reflect.md b/.changeset/yellow-games-reflect.md new file mode 100644 index 0000000000000..ebc7f02e00a47 --- /dev/null +++ b/.changeset/yellow-games-reflect.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Product option values in CSV Export diff --git a/packages/medusa/src/strategies/batch-jobs/product/export.ts b/packages/medusa/src/strategies/batch-jobs/product/export.ts index d0f4e7d5380c8..d073adf58047f 100644 --- a/packages/medusa/src/strategies/batch-jobs/product/export.ts +++ b/packages/medusa/src/strategies/batch-jobs/product/export.ts @@ -497,11 +497,17 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy { const columnNameNameValue = columnNameValueBuilder(i) + // option values are not guaranteed to keep the same order as options on product. Pick them in the same order as product options this.columnsDefinition[columnNameNameValue] = { name: columnNameNameValue, exportDescriptor: { - accessor: (variant: ProductVariant) => - variant?.options[i]?.value ?? "", + accessor: ( + variant: ProductVariant, + context?: { product?: Product } + ) => + variant?.options.find( + (ov) => ov.option_id === context!.product!.options[i]?.id + )?.value ?? "", entityName: "variant", }, } @@ -597,7 +603,7 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy { } if (columnSchema.entityName === "variant") { const formattedContent = csvCellContentFormatter( - columnSchema.accessor(variant) + columnSchema.accessor(variant, { product: product }) ) variantLineData.push(formattedContent) } diff --git a/packages/medusa/src/strategies/batch-jobs/product/types/index.ts b/packages/medusa/src/strategies/batch-jobs/product/types/index.ts index b5c3cff438bea..bedd1632a1ab7 100644 --- a/packages/medusa/src/strategies/batch-jobs/product/types/index.ts +++ b/packages/medusa/src/strategies/batch-jobs/product/types/index.ts @@ -72,7 +72,10 @@ export type ProductExportDescriptor = entityName: Extract } | { - accessor: (variant: ProductVariant) => string + accessor: ( + variant: ProductVariant, + context?: { product?: Product } + ) => string entityName: Extract }