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";
|
||||
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 { usePetStore } from "../lib/store/usePetStore";
|
||||
import { productService } from "../lib/services/productService";
|
||||
@@ -205,7 +205,7 @@ export const ArchiveProductCard: React.FC<{ product: Product }> = ({ product })
|
||||
</div>
|
||||
|
||||
<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>
|
||||
@@ -294,7 +294,7 @@ const ArchiveProductListItem: React.FC<{ product: Product }> = ({ product }) =>
|
||||
return null;
|
||||
}, [product, activePet]);
|
||||
|
||||
const isOutOfStock = (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) <= 0;
|
||||
const isOutOfStock = isProductOutOfStock(product);
|
||||
const { showPrices, allowCart, showPreorderBtn } = useCatalogMode();
|
||||
|
||||
return (
|
||||
|
||||
@@ -14,6 +14,7 @@ import { useCartStore } from "../lib/store/cartStore";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { useCatalogMode } from "../lib/useCatalogMode";
|
||||
import { isProductOutOfStock } from "../lib/utils";
|
||||
import { isSuitableForDog, isSuitableForCat, isSuitableForBoth, isSpeciesCompatible, matchesMedicalSymptom } from "../lib/petCompatibility";
|
||||
|
||||
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" />
|
||||
</div>
|
||||
{allowCart && (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) > 0 && (
|
||||
{allowCart && !isProductOutOfStock(product) && (
|
||||
<div
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
|
||||
@@ -47,7 +47,7 @@ const ICON_MAP: Record<string, React.ComponentType<{ className?: string }>> = {
|
||||
Zap,
|
||||
Star
|
||||
};
|
||||
import { toPersian } from "../lib/utils";
|
||||
import { toPersian, isProductOutOfStock } from "../lib/utils";
|
||||
import { Product } from "../lib/data/products";
|
||||
import { productService } from "../lib/services/productService";
|
||||
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>;
|
||||
|
||||
const isOutOfStock = (product.packageSize !== undefined && product.packageSize !== null ? Number(product.packageSize) : 100) <= 0;
|
||||
const isOutOfStock = isProductOutOfStock(fullProduct || product);
|
||||
|
||||
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">
|
||||
@@ -1420,7 +1420,7 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
||||
<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')} تومان)` : 'ثبت پیشخرید (رایگان)'}
|
||||
</button>
|
||||
) : (product.packageSize || 0) <= 0 ? (
|
||||
) : isOutOfStock ? (
|
||||
<button
|
||||
data-testid="product-desktop-out-of-stock-btn"
|
||||
onClick={() => toast.info("درخواست اطلاعرسانی موجودی ثبت شد")}
|
||||
|
||||
@@ -190,6 +190,7 @@ export class ProductService {
|
||||
metaDescription: (inputData.metaDescription as string) || undefined,
|
||||
canonicalUrl: (inputData.canonicalUrl as string) || 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',
|
||||
noIndex: Boolean(inputData.noIndex),
|
||||
noFollow: Boolean(inputData.noFollow),
|
||||
|
||||
@@ -12,3 +12,16 @@ export function cn(...classes: (string | boolean | undefined)[]) {
|
||||
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