From 245127227e9df55e0a48336c88b959ccb18d8f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Meyer?= Date: Thu, 13 Jul 2023 16:18:09 +0200 Subject: [PATCH] feat(poc): if parent change variant url --- lib/shopware/index.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/shopware/index.ts b/lib/shopware/index.ts index 7af3cfa6e..17d568725 100644 --- a/lib/shopware/index.ts +++ b/lib/shopware/index.ts @@ -116,10 +116,33 @@ export async function getSearchCollectionProducts(params?: { const searchCriteria = { ...criteria, ...sorting }; const res = await requestSearchCollectionProducts(searchCriteria); + res.elements = await changeVariantUrlToParentUrl(res); return res ? transformProducts(res) : []; } +export async function changeVariantUrlToParentUrl( + collection: ExtendedProductListingResult +): Promise { + const newElements: ExtendedProduct[] = []; + if (collection.elements && collection.elements.length > 0) { + await Promise.all( + collection.elements.map(async (item) => { + if (item.parentId && item.seoUrls && item.seoUrls[0]) { + const parentProduct = await getFirstProduct(item.parentId); + if (parentProduct && parentProduct.seoUrls && parentProduct.seoUrls[0]) { + item.seoUrls[0].seoPathInfo = parentProduct.seoUrls[0].seoPathInfo; + } + } + + newElements.push(item); + }) + ); + } + + return newElements; +} + export async function getCollectionProducts(params?: { collection: string; page?: number; @@ -152,6 +175,7 @@ export async function getCollectionProducts(params?: { : params?.defaultSearchCriteria; const productsCriteria = { ...criteria, ...sorting }; res = await requestCategoryProductsCollection(category, productsCriteria); + res.elements = await changeVariantUrlToParentUrl(res); } return res