@php /** @var \App\Models\Product $p */ $productSlug = $p->slug ?? (string) $p->id; $imgUrl = $p->image ? asset('images/products/' . $p->image) : asset('images/products/no-image.png'); $descShort = \Illuminate\Support\Str::limit(strip_tags($p->note ?? ''), 600); $minPrice = (float) ($p->display_price ?? ($p->price ?? 0)); $variants = $p->relationLoaded('variants') ? $p->variants : collect($p->variants ?? []); $variants = collect($variants); $variantPayload = $variants->map(function($v) use ($currency) { $final = (float) ($v->display_price ?? ($v->price ?? 0)); return [ 'id' => (int) ($v->id ?? 0), 'name' => (string) ($v->name ?? ''), 'price' => (float) ($v->price ?? 0), 'display_price' => $final, 'display_price_formatted' => $currency . number_format($final, 2, '.', ','), 'image' => !empty($v->image) ? asset('images/products/' . $v->image) : null, 'stock' => (int) max(0, $v->stock ?? $v->qty ?? 0), ]; })->values(); $productStock = $variants->isEmpty() ? (int) max(0, $p->stock ?? 0) : null; $allowOverselling = isset($s) ? (bool) ($s->allow_overselling ?? true) : true; if ($allowOverselling) { $isAvailable = true; $availabilityLabel = null; } else { if ($variants->isEmpty()) { $isAvailable = $productStock !== null && $productStock > 0; $availabilityLabel = $productStock !== null ? ($productStock > 0 ? __('messages.X_in_stock', ['count' => $productStock]) : __('messages.OutOfStock')) : null; } else { $isAvailable = $variantPayload->contains(fn($v) => ($v['stock'] ?? 0) > 0); $availabilityLabel = $isAvailable ? __('messages.InStock') : __('messages.OutOfStock'); } } @endphp