From 4cf720b63998fb2c47446852f70f53b49a64a27e Mon Sep 17 00:00:00 2001 From: DuvCharles Date: Fri, 17 Nov 2023 08:52:22 +0100 Subject: [PATCH] --wip-- [skip ci] --- components/cart/add-to-cart.tsx | 4 ++-- lib/sylius/index.ts | 7 ++++--- lib/sylius/normalizer/product-normalizer.ts | 1 + lib/sylius/types.ts | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/components/cart/add-to-cart.tsx b/components/cart/add-to-cart.tsx index 9c69c3d80c..c85f0a5341 100644 --- a/components/cart/add-to-cart.tsx +++ b/components/cart/add-to-cart.tsx @@ -18,13 +18,13 @@ export function AddToCart({ const router = useRouter(); const searchParams = useSearchParams(); const [isPending, startTransition] = useTransition(); - const defaultVariantId = variants.length === 1 ? variants[0]?.id : undefined; + const defaultVariantId = variants.length === 1 ? variants[0]?.code : undefined; const variant = variants.find((variant: ProductVariant) => variant.selectedOptions.every( (option) => option.value === searchParams.get(option.name.toLowerCase()) ) ); - const selectedVariantId = variant?.id || defaultVariantId; + const selectedVariantId = variant?.code || defaultVariantId; const title = !availableForSale ? 'Out of stock' : !selectedVariantId diff --git a/lib/sylius/index.ts b/lib/sylius/index.ts index 82c3bf4d34..d3ddd4cd97 100644 --- a/lib/sylius/index.ts +++ b/lib/sylius/index.ts @@ -164,9 +164,10 @@ export const getCart = async (cartId: string): Promise => { const syliusCart = data.body; return normalizeCart(syliusCart); }; -export const addToCart = (cartId: string | undefined, payload: AddToCartPayload[]) => { - syliusRequest(REST_METHODS.PUT, `/orders/${cartId}/items`, payload[0]); - return {}; +export const addToCart = async (cartId: string | undefined, payload: AddToCartPayload[]) => { + console.log('***** DATA *****', payload); + const data = await syliusRequest(REST_METHODS.POST, `/orders/${cartId}/items`, { productVariant: payload[0]?.merchandiseId, quantity: payload[0]?.quantity }); + console.log('***** DATA *****', data.body); }; export const removeFromCart = () => {}; export const updateCart = () => {}; diff --git a/lib/sylius/normalizer/product-normalizer.ts b/lib/sylius/normalizer/product-normalizer.ts index f846fa2085..92e09b9f27 100644 --- a/lib/sylius/normalizer/product-normalizer.ts +++ b/lib/sylius/normalizer/product-normalizer.ts @@ -30,6 +30,7 @@ export const normalizeProduct = (product: SyliusProduct): Product => ({ const normalizeProductVariant = (variant: SyliusProductVariant): ProductVariant => { return { id: variant.id.toString(), + code: variant.code, title: variant.name, availableForSale: variant.inStock, selectedOptions: variant.optionValues.map((optionValue) => { diff --git a/lib/sylius/types.ts b/lib/sylius/types.ts index ddd6a11986..616c05ad49 100644 --- a/lib/sylius/types.ts +++ b/lib/sylius/types.ts @@ -63,6 +63,7 @@ export interface ProductOption { export interface ProductVariant { id: string; + code: string title: string; availableForSale: boolean; selectedOptions: {