From 4cb3bade5d2527703533114d879d046c65f5fa60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Meyer?= Date: Thu, 6 Jul 2023 13:23:10 +0200 Subject: [PATCH] feat(poc): product variants and options --- lib/shopware/transform.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/shopware/transform.ts b/lib/shopware/transform.ts index d0496803f..5b815134b 100644 --- a/lib/shopware/transform.ts +++ b/lib/shopware/transform.ts @@ -143,7 +143,7 @@ export function transformProduct(item: ApiSchemas['Product']): Product { currencyCode: 'EUR' } }, - variants: [], + variants: productVariants, featuredImage: { url: item.cover?.media?.url ?? '', altText: item.cover?.media?.translated?.alt ?? '', @@ -198,6 +198,35 @@ function transformOptions(parent: ApiSchemas['Product']): ProductOption[] { function transformVariants(parent: ApiSchemas['Product']): ProductVariant[] { let productVariants: ProductVariant[] = []; + if (parent.children && parent.parentId === null && parent.children.length > 0) { + parent.children.map((child) => { + if (child.id) { + let selectedOptions: { name: string; value: string }[] = []; + child.options?.map((option) => { + if (option.group) { + selectedOptions.push({ + name: option.group.name, + value: option.name + }); + } + }); + const currentVariant: ProductVariant = { + id: child.id, + title: child.name, + availableForSale: child.available ?? false, + selectedOptions: selectedOptions, + price: { + amount: child.calculatedPrice?.totalPrice + ? String(child.calculatedPrice?.totalPrice) + : '0', + currencyCode: 'EUR' + } + }; + + productVariants.push(currentVariant) + } + }); + } return productVariants; }