|
diff --git a/app/wishlist/page.tsx b/app/wishlist/page.tsx
index 847eec6..c66e43c 100644
--- a/app/wishlist/page.tsx
+++ b/app/wishlist/page.tsx
@@ -15,12 +15,10 @@ import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
import { FloatingButtons } from '@/components/FloatingButtons';
import { useCartStore } from '@/store/cartStore';
import { ProductResponse } from '@/services/api/catalogService';
-import { formatCatalogPriceLabel, getImageUrl } from '@/lib/utils';
+import { formatCatalogPriceLabel, getImageUrl, resolveProductImage } from '@/lib/utils';
function getProductImage(product: ProductResponse): string {
- const fromProduct = product.images?.[0]?.image_url;
- const fromVariant = product.variants?.[0]?.images?.[0]?.image_url;
- return getImageUrl(fromProduct || fromVariant || '');
+ return resolveProductImage(product);
}
function getPriceLabel(product: ProductResponse): string {
diff --git a/components/catalog/ParentFeatureCatalog.tsx b/components/catalog/ParentFeatureCatalog.tsx
index 48b7ccc..7ae2060 100644
--- a/components/catalog/ParentFeatureCatalog.tsx
+++ b/components/catalog/ParentFeatureCatalog.tsx
@@ -17,7 +17,7 @@ import { BrandSelector } from './BrandSelector';
import { SeriesSelector } from './SeriesSelector';
import { ModelSelector } from './ModelSelector';
import { StandardCatalog } from './StandardCatalog';
-import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
+import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
import { ShoppingCart, Heart, ArrowLeftRight, Search, RotateCcw } from 'lucide-react';
import { useCartStore } from '@/store/cartStore';
import { mapProductCardToProductResponse } from '@/utils/productMapper';
@@ -361,7 +361,7 @@ export function ParentFeatureCatalog({ category }: ParentFeatureCatalogProps) {
{products.map((p) => {
const firstVariant = p.variants?.[0];
const { price, oldPrice: comparePrice } = getCatalogPriceInfo(p);
- const primaryImage = getImageUrl(p.images?.[0]?.image_url);
+ const primaryImage = resolveProductImage(p);
const isWishlisted = wishlist.some((item) => item.product_id === p.product_id);
const isCompared = compareList.some((item) => item.product_id === p.product_id);
diff --git a/components/catalog/StandardCatalog.tsx b/components/catalog/StandardCatalog.tsx
index 12b5980..2edefb9 100644
--- a/components/catalog/StandardCatalog.tsx
+++ b/components/catalog/StandardCatalog.tsx
@@ -10,7 +10,7 @@ import {
CategoryResponse,
BrandResponse,
} from '@/services/api/catalogService';
-import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
+import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
import {
applyCatalogFilters,
CATALOG_API_LIMIT,
@@ -90,7 +90,7 @@ function CatalogProductCard({
const discount =
mappedDiscount ||
(compare > price ? Math.round(((compare - price) / compare) * 100) : 0);
- const image = getImageUrl((product as any).thumbnail_url || product.images?.[0]?.image_url);
+ const image = resolveProductImage(product);
const rating = getDisplayStarCount(product);
const isWishlisted = wishlist.some((i) => i.product_id === product.product_id);
const isCompared = compareList.some((i) => i.product_id === product.product_id);
diff --git a/components/pdp/YouMayAlsoLike.tsx b/components/pdp/YouMayAlsoLike.tsx
index 691022e..78df00c 100644
--- a/components/pdp/YouMayAlsoLike.tsx
+++ b/components/pdp/YouMayAlsoLike.tsx
@@ -5,7 +5,7 @@ import Image from 'next/image';
import { useEffect, useState } from 'react';
import BlurHashImage from '@/components/ui/BlurHashImage';
import { ProductResponse } from '@/services/api/catalogService';
-import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
+import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
interface YouMayAlsoLikeProps {
products: ProductResponse[];
@@ -101,12 +101,7 @@ export function YouMayAlsoLike({ products }: YouMayAlsoLikeProps) {
style={{ gridTemplateColumns: `repeat(${Math.min(products.length, 5)}, minmax(168px, 1fr))` }}
>
{products.slice(0, 5).map((prod, index) => {
- const img = getImageUrl(
- prod.thumbnail_url ||
- prod.images?.[0]?.image_url ||
- prod.variants?.[0]?.thumbnail_url ||
- prod.variants?.[0]?.images?.[0]?.image_url
- );
+ const img = resolveProductImage(prod);
const off = discountPct(prod);
const pricing = priceLabel(prod);
const onSale = off > 0;
diff --git a/components/store/CartDrawer.tsx b/components/store/CartDrawer.tsx
index d3f1766..b62d67c 100644
--- a/components/store/CartDrawer.tsx
+++ b/components/store/CartDrawer.tsx
@@ -8,17 +8,17 @@ import { useEffect, useState } from 'react';
import Link from 'next/link';
import { X, Trash2, Plus, Minus, ShoppingBag, ArrowRight, Truck } from 'lucide-react';
import { useCartStore } from '@/store/cartStore';
-import { formatCurrency, getImageUrl } from '@/lib/utils';
+import { formatCurrency, getImageUrl, resolveProductImage } from '@/lib/utils';
import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout';
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
function resolveItemImage(item: {
- product: { images?: { image_url?: string }[]; name?: string };
- selectedVariant: { images?: { image_url?: string }[] };
+ product: any;
+ selectedVariant: any;
}): string {
- const fromVariant = item.selectedVariant?.images?.[0]?.image_url;
- const fromProduct = item.product?.images?.[0]?.image_url;
- return getImageUrl(fromVariant || fromProduct || '');
+ const fromVariant = item.selectedVariant?.thumbnail_url || item.selectedVariant?.images?.[0]?.image_url;
+ if (fromVariant) return getImageUrl(fromVariant);
+ return resolveProductImage(item.product);
}
export function CartDrawer() {
diff --git a/components/store/ProductCard.tsx b/components/store/ProductCard.tsx
index 07d3ba2..ca61a1c 100644
--- a/components/store/ProductCard.tsx
+++ b/components/store/ProductCard.tsx
@@ -14,7 +14,7 @@ import { useRouter } from 'next/navigation';
import { Star, Eye, Heart, ArrowLeftRight, ShoppingBag, Check } from 'lucide-react';
import { ProductResponse } from '@/services/api/catalogService';
import { useCartStore } from '@/store/cartStore';
-import { formatCurrency, getImageUrl } from '@/lib/utils';
+import { formatCurrency, getImageUrl, resolveProductImage } from '@/lib/utils';
import BlurHashImage from '@/components/ui/BlurHashImage';
import { addToCompareAndNavigate } from '@/lib/compare';
@@ -31,9 +31,9 @@ export function ProductCard({ product }: ProductCardProps) {
const setQuickViewProduct = useCartStore((state) => state.setQuickViewProduct);
const router = useRouter();
- const primaryVariant = product.variants[0];
- const primaryImage = getImageUrl(product.images[0]?.image_url);
- const secondaryImage = getImageUrl(product.images[1]?.image_url) || primaryImage;
+ const primaryVariant = product.variants?.[0];
+ const primaryImage = resolveProductImage(product);
+ const secondaryImage = getImageUrl(product.images?.[1]?.image_url) || primaryImage;
const productUrl = (product as any).variant_url || `/products/${product.slug}`;
const price = Number(primaryVariant?.price || 0);
diff --git a/lib/utils.ts b/lib/utils.ts
index ec2ee3f..02fe0a9 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -93,8 +93,18 @@ export function getImageUrl(path?: string | null | unknown): string {
if (typeof path === 'string') {
resolvedPath = path;
} else if (path && typeof path === 'object') {
- const obj = path as Record;
- resolvedPath = String(obj.image_url ?? obj.url ?? obj.src ?? '');
+ const obj = path as Record;
+ resolvedPath = String(
+ obj.thumbnail_url ??
+ obj.image_url ??
+ obj.image ??
+ (Array.isArray(obj.images) && obj.images.length > 0
+ ? (typeof obj.images[0] === 'string' ? obj.images[0] : (obj.images[0]?.image_url ?? ''))
+ : '') ??
+ obj.url ??
+ obj.src ??
+ ''
+ );
} else {
return '/images/placeholder-product.webp';
}
@@ -153,3 +163,22 @@ export function getImageUrl(path?: string | null | unknown): string {
return cleanPath;
}
+/**
+ * Universal bulletproof image resolver for any product or variant representation.
+ * Prioritizes thumbnail_url, product.image, variant images, product images, and imageHover.
+ */
+export function resolveProductImage(product?: any): string {
+ if (!product) return '/images/placeholder-product.webp';
+ const raw =
+ product.thumbnail_url ||
+ product.image ||
+ product.images?.[0]?.image_url ||
+ (typeof product.images?.[0] === 'string' ? product.images[0] : null) ||
+ product.variants?.[0]?.thumbnail_url ||
+ product.variants?.[0]?.images?.[0]?.image_url ||
+ (typeof product.variants?.[0]?.images?.[0] === 'string' ? product.variants[0].images[0] : null) ||
+ product.imageHover;
+ return getImageUrl(raw);
+}
+
+
diff --git a/services/api/catalogService.ts b/services/api/catalogService.ts
index 24d8347..42f8953 100644
--- a/services/api/catalogService.ts
+++ b/services/api/catalogService.ts
@@ -263,7 +263,7 @@ export const catalogService = {
category_id: p.category_id,
slug: p.slug,
name: p.name,
- thumbnail_url: p.images[0]?.image_url || null,
+ thumbnail_url: (p as any).thumbnail_url || p.images?.[0]?.image_url || null,
price,
compare_price,
discount_percent,
diff --git a/utils/productMapper.ts b/utils/productMapper.ts
index fb79156..c05a166 100644
--- a/utils/productMapper.ts
+++ b/utils/productMapper.ts
@@ -105,11 +105,16 @@ export function mapProductCardToProductResponse(
export function mapCatalogProductToProduct(
p: ProductResponse | ProductCardResponse | Record
): Product {
+ const cardMapped = mapProductCardToProductResponse(p);
const normalized: ProductResponse =
Array.isArray((p as any).variants) && (p as any).variants.length > 0
? ({
- ...mapProductCardToProductResponse(p),
+ ...cardMapped,
...p,
+ images:
+ Array.isArray((p as any).images) && (p as any).images.length > 0
+ ? (p as any).images
+ : cardMapped.images,
variants: (p as any).variants.map((v: any) => ({
...v,
price: parseMoney(v.price),
@@ -119,7 +124,7 @@ export function mapCatalogProductToProduct(
: null,
})),
} as ProductResponse)
- : mapProductCardToProductResponse(p);
+ : cardMapped;
const { price, priceMax, oldPrice, discount } = getCatalogPriceInfo(normalized);
|