where('active', 0) ->pluck('name'); } public function filterProductsByVisibleCategories(Collection $products): Collection { $hidden = $this->hiddenCategoryNames(); if ($hidden->isEmpty()) { return $products; } return $products->filter(function ($product) use ($hidden) { $group = trim((string) ($product->group_type ?? '')); if ($group === '') { return true; } if (! Category::where('name', $group)->exists()) { return true; } return ! $hidden->contains($group); })->values(); } public function sortProductsForStore(Collection $products): Collection { return $products ->sort(function ($a, $b) { $aInStock = (int) ($a->inventory ?? 0) > 0 ? 0 : 1; $bInStock = (int) ($b->inventory ?? 0) > 0 ? 0 : 1; if ($aInStock !== $bInStock) { return $aInStock <=> $bInStock; } return ((int) ($b->inventory ?? 0)) <=> ((int) ($a->inventory ?? 0)); }) ->values(); } public function syncFromProducts(): int { $fromProducts = Product::query() ->whereNotNull('group_type') ->where('group_type', '!=', '') ->distinct() ->pluck('group_type'); $names = collect(self::DEFAULT_CATEGORY_NAMES) ->merge($fromProducts) ->map(static fn ($name) => trim((string) $name)) ->filter() ->unique() ->values(); $created = 0; $order = (int) Category::max('sort_order'); foreach ($names as $name) { if (Category::where('name', $name)->exists()) { continue; } $order++; Category::create([ 'name' => $name, 'active' => 1, 'show_in_filter' => 1, 'show_in_select' => 1, 'sort_order' => $order, ]); $created++; } return $created; } }