From 8bfd7afdcb46ca5b4cd83ad4f6ac26bdca32e59e Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Tue, 19 May 2026 16:04:22 +0200 Subject: [PATCH] fix(product): fix cart deduplication for products without variants addToCart() compared item.variantId === selectedVariant where selectedVariant is null but saved items store variantId: undefined. null !== undefined caused findIndex to never match, always inserting a duplicate instead of incrementing quantity. Normalised with || undefined. --- app/src/app/products/[slug]/page.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/app/products/[slug]/page.tsx b/app/src/app/products/[slug]/page.tsx index 2aeb14b..1eeecc0 100644 --- a/app/src/app/products/[slug]/page.tsx +++ b/app/src/app/products/[slug]/page.tsx @@ -54,7 +54,7 @@ export default function ProductDetailPage() { const cart = JSON.parse(localStorage.getItem('cart') || '[]') const existingIndex = cart.findIndex( (item: { productId: string; variantId?: string }) => - item.productId === product.id && item.variantId === selectedVariant + item.productId === product.id && item.variantId === (selectedVariant || undefined) ) if (existingIndex >= 0) { @@ -249,7 +249,12 @@ export default function ProductDetailPage() { size="lg" variant="secondary" onClick={() => { - addToCart() + const cart = JSON.parse(localStorage.getItem('cart') || '[]') + const alreadyInCart = cart.some( + (item: { productId: string; variantId?: string }) => + item.productId === product.id && item.variantId === (selectedVariant || undefined) + ) + if (!alreadyInCart) addToCart() router.push('/cart') }} >