{{-- Template Name: Trgovina (Elit Gastro) Brand-styled product catalog page. Renders a branded hero, a sticky toolbar (search + category chips + sort), and the WooCommerce products grid. If the page editor has its own content (e.g. a [products] shortcode or Products block) it's rendered as-is below the toolbar. Otherwise the template auto-renders [products] with the current orderby / category from the URL — so the sort dropdown and category chips work out of the box. --}} @extends('layouts.app') @section('content') @php /* --------------------------------------------------------------- * URL state — orderby + active category + search query * --------------------------------------------------------------- */ $sortKey = sanitize_key($_GET['orderby'] ?? 'menu_order'); $sortMap = [ 'menu_order' => ['orderby' => 'menu_order', 'order' => 'asc', 'label' => __('Zadano', 'elit-gastro')], 'date' => ['orderby' => 'date', 'order' => 'desc', 'label' => __('Najnovije', 'elit-gastro')], 'price' => ['orderby' => 'price', 'order' => 'asc', 'label' => __('Cijena: rastuće', 'elit-gastro')], 'price-desc' => ['orderby' => 'price', 'order' => 'desc', 'label' => __('Cijena: padajuće', 'elit-gastro')], 'popularity' => ['orderby' => 'popularity', 'order' => 'desc', 'label' => __('Najpopularnije', 'elit-gastro')], 'rating' => ['orderby' => 'rating', 'order' => 'desc', 'label' => __('Najbolje ocjene', 'elit-gastro')], ]; $current = $sortMap[$sortKey] ?? $sortMap['menu_order']; $searchQ = sanitize_text_field(wp_unslash($_GET['s'] ?? '')); $activeCatSlug = sanitize_key($_GET['product_cat'] ?? ''); /* --------------------------------------------------------------- * Top-level product categories (chips) * --------------------------------------------------------------- */ $shopCats = []; if (taxonomy_exists('product_cat')) { $terms = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => true, 'parent' => 0, 'orderby' => 'menu_order', 'number' => 12, 'exclude' => [(int) get_option('default_product_cat')], ]); if (! is_wp_error($terms) && ! empty($terms)) { $shopCats = $terms; } } /* --------------------------------------------------------------- * Walk the main loop once to capture everything we need * (title, excerpt, hero image, custom editor content). After this * block, we rely on the captured variables — not the global $post. * --------------------------------------------------------------- */ $pageTitle = ''; $pageExcerpt = ''; $heroImg = null; $hasCustomContent = false; $customContent = ''; if (have_posts()) { while (have_posts()) { the_post(); $pageTitle = get_the_title(); $pageExcerpt = has_excerpt() ? get_the_excerpt() : ''; if (has_post_thumbnail()) { $heroImg = get_the_post_thumbnail_url(null, 'large') ?: null; } $customContent = trim((string) get_the_content()); $hasCustomContent = ($customContent !== ''); } wp_reset_postdata(); } /* --------------------------------------------------------------- * Build the [products] shortcode args from URL state * --------------------------------------------------------------- */ $atts = [ 'limit' => 12, 'columns' => 4, 'paginate' => 'true', 'orderby' => $current['orderby'], 'order' => $current['order'], ]; if ($activeCatSlug) { $atts['category'] = $activeCatSlug; } $shortcode = '[products'; foreach ($atts as $k => $v) { $shortcode .= ' '.$k.'="'.esc_attr($v).'"'; } $shortcode .= ']'; $pageUrl = get_permalink(); $pagedNow = max(1, (int) get_query_var('paged'), (int) get_query_var('page')); @endphp
@if ($heroImg) @endif
{{ __('Katalog', 'elit-gastro') }} @if ($activeCatSlug) @php($activeTerm = get_term_by('slug', $activeCatSlug, 'product_cat')) @if ($activeTerm) · {{ $activeTerm->name }} @endif @endif

{{ $pageTitle ?: __('Trgovina', 'elit-gastro') }}

@if ($pageExcerpt)

{{ $pageExcerpt }}

@else

{{ __('Profesionalna ugostiteljska oprema za hotele, restorane i rekreacijske centre — od termičkih linija LAINOX do Scotsman ledomata.', 'elit-gastro') }}

@endif
@include('partials.shop-toolbar', [ 'baseUrl' => $pageUrl, 'searchQ' => $searchQ, 'activeCatSlug' => $activeCatSlug, 'cats' => $shopCats, 'showSort' => true, 'sortMap' => $sortMap, 'sortKey' => $sortKey, 'useTermLinks' => false, ])
{{-- Active filter pill (lets user clear category) --}} @if ($activeCatSlug || $searchQ)
{{ __('Aktivni filter:', 'elit-gastro') }} @if ($activeCatSlug) @php($activeTerm = get_term_by('slug', $activeCatSlug, 'product_cat')) @if ($activeTerm) {{ $activeTerm->name }} @endif @endif @if ($searchQ) "{{ $searchQ }}" @endif
@endif
@if ($hasCustomContent)
{!! apply_filters('the_content', $customContent) !!}
@else @if ($searchQ) {{-- pre_get_posts doesn't run on Page templates, so we render a dedicated WP_Query for product keyword matches. --}} @php $searchQuery = new WP_Query([ 'post_type' => 'product', 'posts_per_page' => 12, 'paged' => $pagedNow, 's' => $searchQ, 'orderby' => $current['orderby'], 'order' => $current['order'], 'tax_query' => $activeCatSlug ? [[ 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $activeCatSlug, ]] : [], ]); @endphp @if ($searchQuery->have_posts())
    @while ($searchQuery->have_posts()) @php($searchQuery->the_post()) @php(wc_get_template_part('content', 'product')) @endwhile
@if ($searchQuery->max_num_pages > 1) @endif @else

{{ sprintf(__('Nema rezultata za "%s"', 'elit-gastro'), $searchQ) }}

{{ __('Pokušaj s drugim pojmom ili pregledaj kategorije iznad.', 'elit-gastro') }}

{{ __('Pokaži sve proizvode', 'elit-gastro') }}
@endif @php(wp_reset_postdata()) @else {!! do_shortcode($shortcode) !!} @endif @endif
@endsection