Skip to content

Commit

Permalink
Merge pull request #2417 from graphcommerce-org/feature/algolia-custo…
Browse files Browse the repository at this point in the history
…m-attributes

Prevent errors by returning string instead of array
  • Loading branch information
paales authored Nov 18, 2024
2 parents 32bb1f7 + 743e7e2 commit 75762ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-apes-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/algolia-products": patch
---

Prevent errors by returning string instead of array
10 changes: 10 additions & 0 deletions packages/algolia-products/mesh/algoliaHitToMagentoProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -187,5 +196,6 @@ export function algoliaHitToMagentoProduct(
url_key: algoliaUrlToUrlKey(url, storeConfig?.base_link_url),
url_suffix: storeConfig?.product_url_suffix,
...rest,
...flattenedCustomAttributes,
}
}

0 comments on commit 75762ad

Please sign in to comment.