use analysisElementMedusaProductIds from order product selected variant if it exists
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
export const getAnalysisElementMedusaProductIds = (products: ({
|
||||
import { StoreProduct } from "@medusajs/types";
|
||||
|
||||
type Product = {
|
||||
metadata?: {
|
||||
analysisElementMedusaProductIds?: string;
|
||||
} | null;
|
||||
} | null)[]) => {
|
||||
variant?: {
|
||||
metadata?: {
|
||||
analysisElementMedusaProductIds?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
} | null;
|
||||
|
||||
export const getAnalysisElementMedusaProductIds = (products: Pick<StoreProduct, 'metadata'>[]) => {
|
||||
if (!products) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const mapped = products
|
||||
.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 {
|
||||
return JSON.parse(value as string);
|
||||
return [...JSON.parse(value as string), ...JSON.parse(value_variant as string)];
|
||||
} catch (e) {
|
||||
console.error("Failed to parse analysisElementMedusaProductIds from analysis package, possibly invalid format", e);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user