fix(storefront): accurately reflect out-of-stock products based on stock quantity and stockStatus
This commit is contained in:
1 parent
6c53126cb6
commit
075f00a4ed
5 files changed
+22
-7
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React, { useState, useMemo, useEffect, useCallback } from "react";
|
import React, { useState, useMemo, useEffect, useCallback } from "react";
|
||||||
import { toPersian } from "../lib/utils";
|
import { toPersian, isProductOutOfStock } from "../lib/utils";
|
||||||
import { Product, PetType } from "../lib/data/products";
|
import { Product, PetType } from "../lib/data/products";
|
||||||
import { usePetStore } from "../lib/store/usePetStore";
|
import { usePetStore } from "../lib/store/usePetStore";
|
||||||
import { productService } from "../lib/services/productService";
|
import { productService } from "../lib/services/productService";
|
||||||
@@ -205,7 +205,7 @@ export const ArchiveProductCard: React.FC<{ product: Product }> = ({ product })
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="shrink-0 mr-auto">
|
<div className="shrink-0 mr-auto">
|
||||||
{(product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) <= 0 ? (
|
{isProductOutOfStock(product) ? (
|
||||||
<span className="px-2.5 py-1.5 rounded-xl bg-rose-50 text-rose-600 border border-rose-100 text-xs font-bold font-vazir whitespace-nowrap inline-block">
|
<span className="px-2.5 py-1.5 rounded-xl bg-rose-50 text-rose-600 border border-rose-100 text-xs font-bold font-vazir whitespace-nowrap inline-block">
|
||||||
ناموجود
|
ناموجود
|
||||||
</span>
|
</span>
|
||||||
@@ -294,7 +294,7 @@ const ArchiveProductListItem: React.FC<{ product: Product }> = ({ product }) =>
|
|||||||
return null;
|
return null;
|
||||||
}, [product, activePet]);
|
}, [product, activePet]);
|
||||||
|
|
||||||
const isOutOfStock = (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) <= 0;
|
const isOutOfStock = isProductOutOfStock(product);
|
||||||
const { showPrices, allowCart, showPreorderBtn } = useCatalogMode();
|
const { showPrices, allowCart, showPreorderBtn } = useCatalogMode();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { useCartStore } from "../lib/store/cartStore";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
import { useCatalogMode } from "../lib/useCatalogMode";
|
import { useCatalogMode } from "../lib/useCatalogMode";
|
||||||
|
import { isProductOutOfStock } from "../lib/utils";
|
||||||
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||||
|
|
||||||
export function ProductCard({ product, priority = false }: { product: Product; priority?: boolean }) {
|
export function ProductCard({ product, priority = false }: { product: Product; priority?: boolean }) {
|
||||||
@@ -116,7 +117,7 @@ export function ProductCard({ product, priority = false }: { product: Product; p
|
|||||||
>
|
>
|
||||||
<Eye className="w-5 h-5" />
|
<Eye className="w-5 h-5" />
|
||||||
</div>
|
</div>
|
||||||
{allowCart && (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) > 0 && (
|
{allowCart && !isProductOutOfStock(product) && (
|
||||||
<div
|
<div
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ const ICON_MAP: Record<string, React.ComponentType<{ className?: string }>> = {
|
|||||||
Zap,
|
Zap,
|
||||||
Star
|
Star
|
||||||
};
|
};
|
||||||
import { toPersian } from "../lib/utils";
|
import { toPersian, isProductOutOfStock } from "../lib/utils";
|
||||||
import { Product } from "../lib/data/products";
|
import { Product } from "../lib/data/products";
|
||||||
import { productService } from "../lib/services/productService";
|
import { productService } from "../lib/services/productService";
|
||||||
import { SCIENTIFIC_TERMS } from "../lib/data/scientificTerms";
|
import { SCIENTIFIC_TERMS } from "../lib/data/scientificTerms";
|
||||||
@@ -402,7 +402,7 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
|||||||
);
|
);
|
||||||
if (!product || !fullProduct || !calculation) return <div className="min-h-screen flex items-center justify-center">محصول یافت نشد</div>;
|
if (!product || !fullProduct || !calculation) return <div className="min-h-screen flex items-center justify-center">محصول یافت نشد</div>;
|
||||||
|
|
||||||
const isOutOfStock = (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) <= 0;
|
const isOutOfStock = isProductOutOfStock(fullProduct || product);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-medical-gray-50 pt-2 pb-36 sm:pb-20 px-4 sm:px-6 lg:px-8 font-vazir" dir="rtl">
|
<div className="min-h-screen bg-medical-gray-50 pt-2 pb-36 sm:pb-20 px-4 sm:px-6 lg:px-8 font-vazir" dir="rtl">
|
||||||
@@ -1420,7 +1420,7 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
|||||||
<Sparkles className="w-5 h-5 mb-1 text-amber-300" />
|
<Sparkles className="w-5 h-5 mb-1 text-amber-300" />
|
||||||
{((product as unknown as { preorderDeposit?: number }).preorderDeposit || 0) > 0 ? `پیشخرید (بیعانه: ${((product as unknown as { preorderDeposit?: number }).preorderDeposit || 0).toLocaleString('fa-IR')} تومان)` : 'ثبت پیشخرید (رایگان)'}
|
{((product as unknown as { preorderDeposit?: number }).preorderDeposit || 0) > 0 ? `پیشخرید (بیعانه: ${((product as unknown as { preorderDeposit?: number }).preorderDeposit || 0).toLocaleString('fa-IR')} تومان)` : 'ثبت پیشخرید (رایگان)'}
|
||||||
</button>
|
</button>
|
||||||
) : (product.packageSize || 0) <= 0 ? (
|
) : isOutOfStock ? (
|
||||||
<button
|
<button
|
||||||
data-testid="product-desktop-out-of-stock-btn"
|
data-testid="product-desktop-out-of-stock-btn"
|
||||||
onClick={() => toast.info("درخواست اطلاعرسانی موجودی ثبت شد")}
|
onClick={() => toast.info("درخواست اطلاعرسانی موجودی ثبت شد")}
|
||||||
|
|||||||
@@ -190,6 +190,7 @@ export class ProductService {
|
|||||||
metaDescription: (inputData.metaDescription as string) || undefined,
|
metaDescription: (inputData.metaDescription as string) || undefined,
|
||||||
canonicalUrl: (inputData.canonicalUrl as string) || undefined,
|
canonicalUrl: (inputData.canonicalUrl as string) || undefined,
|
||||||
keywords: typeof inputData.keywords === 'string' ? inputData.keywords : undefined,
|
keywords: typeof inputData.keywords === 'string' ? inputData.keywords : undefined,
|
||||||
|
stock: typeof inputData.stock === 'number' ? inputData.stock : (inputData.stock !== undefined && inputData.stock !== null ? Number(inputData.stock) : undefined),
|
||||||
stockStatus: (inputData.stockStatus as 'IN_STOCK' | 'OUT_OF_STOCK' | 'PRE_ORDER' | 'DISCONTINUED') || 'IN_STOCK',
|
stockStatus: (inputData.stockStatus as 'IN_STOCK' | 'OUT_OF_STOCK' | 'PRE_ORDER' | 'DISCONTINUED') || 'IN_STOCK',
|
||||||
noIndex: Boolean(inputData.noIndex),
|
noIndex: Boolean(inputData.noIndex),
|
||||||
noFollow: Boolean(inputData.noFollow),
|
noFollow: Boolean(inputData.noFollow),
|
||||||
|
|||||||
@@ -12,3 +12,16 @@ export function cn(...classes: (string | boolean | undefined)[]) {
|
|||||||
return classes.filter(Boolean).join(" ");
|
return classes.filter(Boolean).join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function isProductOutOfStock(product?: {
|
||||||
|
stock?: number;
|
||||||
|
stockStatus?: "IN_STOCK" | "OUT_OF_STOCK" | "PRE_ORDER" | "DISCONTINUED" | string;
|
||||||
|
packageSize?: number | string;
|
||||||
|
} | null): boolean {
|
||||||
|
if (!product) return false;
|
||||||
|
if (product.stockStatus === "OUT_OF_STOCK" || product.stockStatus === "DISCONTINUED") return true;
|
||||||
|
if (typeof product.stock === "number" && product.stock <= 0) return true;
|
||||||
|
if (product.stock !== undefined && product.stock !== null && Number(product.stock) <= 0) return true;
|
||||||
|
if (product.packageSize !== undefined && product.packageSize !== null && Number(product.packageSize) <= 0) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user