use analysisElementMedusaProductIds from order product selected variant if it exists

This commit is contained in:
2025-09-09 01:16:23 +03:00
parent 596d0e9eee
commit fd94320295
2 changed files with 25 additions and 10 deletions

View File

@@ -714,7 +714,12 @@ export async function getOrderedAnalysisIds({
throw new Error(`Got ${orderedPackagesProducts.length} ordered packages products, expected ${orderedPackageIds.length}`); throw new Error(`Got ${orderedPackagesProducts.length} ordered packages products, expected ${orderedPackageIds.length}`);
} }
const ids = getAnalysisElementMedusaProductIds(orderedPackagesProducts); const ids = getAnalysisElementMedusaProductIds(
orderedPackagesProducts.map(({ id, metadata }) => ({
metadata,
variant: orderedPackages.find(({ product }) => product?.id === id)?.variant,
})),
);
if (ids.length === 0) { if (ids.length === 0) {
return []; return [];
} }

View File

@@ -1,17 +1,27 @@
export const getAnalysisElementMedusaProductIds = (products: ({ import { StoreProduct } from "@medusajs/types";
type Product = {
metadata?: { metadata?: {
analysisElementMedusaProductIds?: string; analysisElementMedusaProductIds?: string;
} | null; } | null;
} | null)[]) => { variant?: {
metadata?: {
analysisElementMedusaProductIds?: string;
} | null;
} | null;
} | null;
export const getAnalysisElementMedusaProductIds = (products: Pick<StoreProduct, 'metadata'>[]) => {
if (!products) { if (!products) {
return []; return [];
} }
const mapped = products const mapped = products
.flatMap((product) => { .flatMap((product) => {
const value = product?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"'); const value = (product as Product)?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"');
const value_variant = (product as Product)?.variant?.metadata?.analysisElementMedusaProductIds?.replaceAll("'", '"');
try { try {
return JSON.parse(value as string); return [...JSON.parse(value as string), ...JSON.parse(value_variant as string)];
} catch (e) { } catch (e) {
console.error("Failed to parse analysisElementMedusaProductIds from analysis package, possibly invalid format", e); console.error("Failed to parse analysisElementMedusaProductIds from analysis package, possibly invalid format", e);
return []; return [];