diff --git a/README.md b/README.md index aefe269f4..21266c857 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ A Next.js 13 and App Router-ready ecommerce template featuring: - Styling with Tailwind CSS - Automatic light/dark mode based on system settings -> Note: Looking for Next.js Commerce v1? View the [code](https://github.com/vercel/commerce/tree/v1), [demo](https://commerce-v1.vercel.store), and [release notes](https://github.com/vercel/commerce/releases/tag/v1) +

+ +> Note: Looking for Next.js Commerce v1? View the [code](https://github.com/vercel/commerce/tree/v1), [demo](https://commerce-v1.vercel.store), and [release notes](https://github.com/vercel/commerce/releases/tag/v1). ## Prerequisites diff --git a/components/cart/actions.ts b/components/cart/actions.ts index f11328cfd..384c0a623 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -53,7 +53,7 @@ export const addItem = async (variantId: string | undefined): Promise => { const cartId = cookies().get('sw-context-token')?.value; diff --git a/components/cart/add-to-cart.tsx b/components/cart/add-to-cart.tsx index dc36182c1..368d5521e 100644 --- a/components/cart/add-to-cart.tsx +++ b/components/cart/add-to-cart.tsx @@ -6,7 +6,7 @@ import { addItem } from 'components/cart/actions'; import LoadingDots from 'components/loading-dots'; import { ProductVariant, Product } from 'lib/shopware/types'; import { useRouter, useSearchParams } from 'next/navigation'; -import { useEffect, useState, useTransition } from 'react'; +import { useTransition } from 'react'; export function AddToCart({ product, @@ -17,34 +17,36 @@ export function AddToCart({ availableForSale: boolean; product: Product; }) { - const [selectedVariantId, setSelectedVariantId] = useState(product.id); const router = useRouter(); const searchParams = useSearchParams(); const [isPending, startTransition] = useTransition(); - - useEffect(() => { - const variant = variants.find((variant: ProductVariant) => - variant.selectedOptions.every( - (option) => option.value === searchParams.get(option.name.toLowerCase()) - ) - ); - - if (variant) { - setSelectedVariantId(variant.id); - } - }, [searchParams, variants, setSelectedVariantId, selectedVariantId]); + const defaultVariantId = variants.length === 1 ? variants[0]?.id : product.id; + const variant = variants.find((variant: ProductVariant) => + variant.selectedOptions.every( + (option) => option.value === searchParams.get(option.name.toLowerCase()) + ) + ); + const selectedVariantId = variant?.id || defaultVariantId; + const title = !availableForSale + ? 'Out of stock' + : !selectedVariantId + ? 'Please select options' + : undefined; return (