mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
fix price
This commit is contained in:
parent
b18c017fff
commit
8756a1b894
@ -20,6 +20,8 @@ import {
|
|||||||
ProductVariant
|
ProductVariant
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
|
const CURRENCY_CODE = 'eur';
|
||||||
|
|
||||||
const payload = new Payload({ baseUrl: process.env.CMS_URL });
|
const payload = new Payload({ baseUrl: process.env.CMS_URL });
|
||||||
|
|
||||||
const reshapeCartItems = (lines: PayloadCart['lines']): CartItem[] => {
|
const reshapeCartItems = (lines: PayloadCart['lines']): CartItem[] => {
|
||||||
@ -170,10 +172,7 @@ const reshapeImage = (media: PayloadMedia): Image => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type Price = {
|
type Price = { amount: number; currencyCode: string };
|
||||||
amount: number;
|
|
||||||
currencyCode: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const reshapePrice = (price: Price): Money => {
|
const reshapePrice = (price: Price): Money => {
|
||||||
return {
|
return {
|
||||||
@ -195,7 +194,7 @@ const reshapeOptions = (variants: PayloadProduct['variants']): ProductOption[] =
|
|||||||
return Array.from(options, ([id, option]) => ({
|
return Array.from(options, ([id, option]) => ({
|
||||||
id,
|
id,
|
||||||
name: option.name,
|
name: option.name,
|
||||||
values: option.values.map((value) => value.label)
|
values: option.values.map(({ label }) => label)
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -212,16 +211,20 @@ const reshapeSelectedOption = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const reshapeVariants = (variants: PayloadProduct['variants']): ProductVariant[] => {
|
const reshapeVariants = (variants: PayloadProduct['variants']): ProductVariant[] => {
|
||||||
return variants.map((variant) => ({
|
return variants
|
||||||
id: variant.id!,
|
.filter((variant) => variant.price.currencyCode === CURRENCY_CODE)
|
||||||
title: `${variant.price.amount} ${variant.price.currencyCode}`,
|
.map((variant) => ({
|
||||||
availableForSale: true,
|
id: variant.id!,
|
||||||
selectedOptions: reshapeSelectedOption(variant.selectedOptions),
|
title: `${variant.price.amount} ${variant.price.currencyCode}`,
|
||||||
price: reshapePrice(variant.price)
|
availableForSale: true,
|
||||||
}));
|
selectedOptions: reshapeSelectedOption(variant.selectedOptions),
|
||||||
|
price: reshapePrice(variant.price)
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const reshapeProduct = (product: PayloadProduct): Product => {
|
const reshapeProduct = (product: PayloadProduct): Product => {
|
||||||
|
const sortedVariants = product.variants.sort((a, b) => a.price.amount - b.price.amount);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: product.id,
|
id: product.id,
|
||||||
handle: product.id,
|
handle: product.id,
|
||||||
@ -231,8 +234,8 @@ const reshapeProduct = (product: PayloadProduct): Product => {
|
|||||||
descriptionHtml: product.description,
|
descriptionHtml: product.description,
|
||||||
options: reshapeOptions(product.variants),
|
options: reshapeOptions(product.variants),
|
||||||
priceRange: {
|
priceRange: {
|
||||||
maxVariantPrice: reshapePrice(product.variants[0]?.price!),
|
maxVariantPrice: reshapePrice(sortedVariants.at(0)?.price!),
|
||||||
minVariantPrice: reshapePrice(product.variants[0]?.price!)
|
minVariantPrice: reshapePrice(sortedVariants.at(-1)?.price!)
|
||||||
},
|
},
|
||||||
featuredImage: reshapeImage(product.media as PayloadMedia),
|
featuredImage: reshapeImage(product.media as PayloadMedia),
|
||||||
images: [reshapeImage(product.media as PayloadMedia)],
|
images: [reshapeImage(product.media as PayloadMedia)],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user