{{--
Primary-navigation menu, used by both the desktop header and the mobile
drawer. If the user assigned a menu to `primary_navigation`, render that;
otherwise render a sensible fallback list.
Inputs:
$menuClass — class to put on the rendered
--}}
@php
$menuClass = $menuClass ?? 'nav-links';
@endphp
@if (has_nav_menu('primary_navigation'))
{{--
`depth => 0` (default) so the menu renders any nested levels the editor
has built in Appearance → Menus. Standard dropdowns + mega menus are
styled in app.css under "Navigation submenus + mega menu". To make a
top-level item open as a wide mega-menu panel, add the CSS class
`mega-menu` to it via the menu item's Screen Options → CSS Classes
field.
--}}
{!! wp_nav_menu([
'theme_location' => 'primary_navigation',
'menu_class' => $menuClass,
'container' => false,
'echo' => false,
'fallback_cb' => false,
]) !!}
@else
{{--
Demo fallback — shown only until the editor creates a menu and assigns
it to the `primary_navigation` location.
The "Proizvodi" item is a MEGA-MENU. Its content is sourced in this
order of preference:
1. Real WooCommerce categories — if there are 2+ top-level
`product_cat` terms in the database, each becomes a column and
its direct child terms become the column's links. So the demo
tracks the actual shop structure as soon as products are added.
2. Static fallback — used only when WC isn't active, no top-level
categories exist, or only one does (one column looks empty).
The whole demo vanishes once a real menu is assigned to the location.
--}}
@php
$shopUrl = (function_exists('wc_get_page_id') && wc_get_page_id('shop') > 0)
? get_permalink(wc_get_page_id('shop'))
: $siteUrl.'#kategorije';
// Mega-menu columns are sourced from real WC categories and split by
// structure to avoid the "empty column under a header" look:
// $branchCols → parents WITH child terms (each gets its own column)
// $leafItems → parents WITHOUT child terms (combined into one
// "Ostale kategorije" column at the end)
// Both fall through to a static demo set if WC isn't active or no
// top-level product_cat terms exist.
$branchCols = [];
$leafItems = [];
if (taxonomy_exists('product_cat')) {
$tops = get_terms([
'taxonomy' => 'product_cat',
'parent' => 0,
'hide_empty' => false,
'orderby' => 'menu_order',
'exclude' => [(int) get_option('default_product_cat')],
]);
if (! is_wp_error($tops)) {
foreach ($tops as $top) {
$topLink = get_term_link($top);
if (is_wp_error($topLink)) {
continue;
}
$children = get_terms([
'taxonomy' => 'product_cat',
'parent' => $top->term_id,
'hide_empty' => false,
'number' => 8,
'orderby' => 'menu_order',
]);
$childLinks = [];
if (! is_wp_error($children)) {
foreach ($children as $child) {
$childLink = get_term_link($child);
if (is_wp_error($childLink)) {
continue;
}
$childLinks[] = ['label' => $child->name, 'url' => $childLink];
}
}
if (! empty($childLinks)) {
$branchCols[] = [
'label' => $top->name,
'url' => $topLink,
'children' => $childLinks,
];
} else {
$leafItems[] = [
'label' => $top->name,
'url' => $topLink,
];
}
}
}
// Keep the panel readable — at most 3 branch columns. Any branches
// beyond that get demoted into the "Ostale kategorije" column.
if (count($branchCols) > 3) {
$overflow = array_slice($branchCols, 3);
$branchCols = array_slice($branchCols, 0, 3);
foreach ($overflow as $col) {
$leafItems[] = ['label' => $col['label'], 'url' => $col['url']];
}
}
}
$hasLiveMega = ! empty($branchCols) || count($leafItems) >= 2;
@endphp
{{-- Mega-menu showcase: WC-driven when possible, static otherwise --}}
@if ($hasLiveMega)
{{-- Branch columns: one column per parent that actually has
child categories. The column heading links to the parent's
archive; the bullet list shows its direct children, capped
by a small "Pogledaj sve →" CTA back to the parent. --}}
@foreach ($branchCols as $col)
@endforeach
{{-- Leaf column: parents that have no children of their own get
grouped here so each one stays a clickable link without
wasting a full mega-column on an empty header. --}}
@if (! empty($leafItems))