feat(poc): product variants and options

This commit is contained in:
Björn Meyer 2023-07-06 13:23:10 +02:00
parent c9cf57c933
commit 4cb3bade5d

View File

@ -143,7 +143,7 @@ export function transformProduct(item: ApiSchemas['Product']): Product {
currencyCode: 'EUR' currencyCode: 'EUR'
} }
}, },
variants: [], variants: productVariants,
featuredImage: { featuredImage: {
url: item.cover?.media?.url ?? '', url: item.cover?.media?.url ?? '',
altText: item.cover?.media?.translated?.alt ?? '', altText: item.cover?.media?.translated?.alt ?? '',
@ -198,6 +198,35 @@ function transformOptions(parent: ApiSchemas['Product']): ProductOption[] {
function transformVariants(parent: ApiSchemas['Product']): ProductVariant[] { function transformVariants(parent: ApiSchemas['Product']): ProductVariant[] {
let productVariants: 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; return productVariants;
} }