diff --git a/.changeset/witty-apes-rescue.md b/.changeset/witty-apes-rescue.md new file mode 100644 index 0000000000..33c1bbc7c1 --- /dev/null +++ b/.changeset/witty-apes-rescue.md @@ -0,0 +1,5 @@ +--- +"@graphcommerce/algolia-products": patch +--- + +Prevent errors by returning string instead of array diff --git a/packages/algolia-products/mesh/algoliaHitToMagentoProduct.ts b/packages/algolia-products/mesh/algoliaHitToMagentoProduct.ts index 5fb284bacb..6ec97f57b3 100644 --- a/packages/algolia-products/mesh/algoliaHitToMagentoProduct.ts +++ b/packages/algolia-products/mesh/algoliaHitToMagentoProduct.ts @@ -147,6 +147,15 @@ export function algoliaHitToMagentoProduct( ...rest } = additionalProperties + // Some custom attributes are returned as array while they need to be a string. Flatten those arrays + const flattenedCustomAttributes = {} + for (const [key, value] of Object.entries(rest)) { + if (value !== null && Array.isArray(value) && value?.length > 0) { + flattenedCustomAttributes[key] = value.toString() + delete rest[key] + } + } + return { redirect_code: 0, __typename: algoliaTypeToTypename[type_id as keyof typeof algoliaTypeToTypename], @@ -187,5 +196,6 @@ export function algoliaHitToMagentoProduct( url_key: algoliaUrlToUrlKey(url, storeConfig?.base_link_url), url_suffix: storeConfig?.product_url_suffix, ...rest, + ...flattenedCustomAttributes, } }