Breadcrumbs: always start at Home, full category trail, BreadcrumbList JSON-LD
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d7f92ebfc
commit
fb239a97cb
8 files changed
+114
-18
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Catalog\CollectionTrail;
|
||||
use App\Catalog\ProductListing;
|
||||
use App\Catalog\ProductListingPage;
|
||||
use Illuminate\Http\Response;
|
||||
@@ -12,6 +13,7 @@ class CategoryController extends Controller
|
||||
public function __construct(
|
||||
private readonly ProductListingPage $listingPage,
|
||||
private readonly CollectionService $collections,
|
||||
private readonly CollectionTrail $trail,
|
||||
) {}
|
||||
|
||||
public function show(string $locale, int $collection)
|
||||
@@ -27,6 +29,10 @@ public function show(string $locale, int $collection)
|
||||
$collectionData['id'],
|
||||
);
|
||||
|
||||
return view('category.show', [...$data, 'collection' => $collectionData]);
|
||||
return view('category.show', [
|
||||
...$data,
|
||||
'collection' => $collectionData,
|
||||
'breadcrumb' => $this->trail->for($collectionData, linkLast: false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Catalog\CollectionTrail;
|
||||
use App\Catalog\ProductListing;
|
||||
use App\Catalog\ProductListingPage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -11,6 +12,7 @@
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Lunar\Models\Product;
|
||||
use Lunar\Models\ProductVariant;
|
||||
use Modules\Core\Catalog\Services\CollectionService;
|
||||
use Modules\Core\Catalog\Services\ProductService;
|
||||
use Modules\Core\Review\Models\ProductReview;
|
||||
|
||||
@@ -19,6 +21,8 @@ class ProductController extends Controller
|
||||
public function __construct(
|
||||
private readonly ProductService $products,
|
||||
private readonly ProductListingPage $listingPage,
|
||||
private readonly CollectionService $collections,
|
||||
private readonly CollectionTrail $trail,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -44,18 +48,40 @@ public function show(string $locale, int $id)
|
||||
|
||||
$product = $this->mergeJustSubmittedReview($product);
|
||||
|
||||
$collection = $product['collections'][0] ?? null;
|
||||
// dd($product);
|
||||
[$productOptions, $variantsData] = $this->buildOptionPicker($id);
|
||||
|
||||
return view('product.show', [
|
||||
'collection' => $collection,
|
||||
'breadcrumb' => [...$this->collectionTrail($product), ['label' => $product['name']]],
|
||||
'product' => $product,
|
||||
'productOptions' => $productOptions,
|
||||
'variantsData' => $variantsData,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The product's first directly-assigned collection with its ancestors, or
|
||||
* the all-products page when it has none. The product document's own
|
||||
* `collections` entries carry neither ancestors nor a current-locale name,
|
||||
* so the collection is re-read from the collection index.
|
||||
*
|
||||
* @return array<int, array{label: string, href?: string}>
|
||||
*/
|
||||
private function collectionTrail(array $product): array
|
||||
{
|
||||
$assigned = $product['collections'][0] ?? null;
|
||||
$collection = $assigned !== null ? $this->collections->getById((int) $assigned['id']) : null;
|
||||
|
||||
if ($collection === null) {
|
||||
return [['label' => __('storefront.nav.products'), 'href' => route('products')]];
|
||||
}
|
||||
|
||||
// Untranslated collections resolve to a null name; the product's own
|
||||
// index-time name is the fallback, same as CollectionTrail's ancestors.
|
||||
$collection['name'] ??= $assigned['name'];
|
||||
|
||||
return $this->trail->for($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* One button/swatch group per product option (a product can have several
|
||||
* — e.g. a custom-photo product with size + style + person-count, each
|
||||
|
||||
Reference in new issue
Block a user