# product/category

> Trang một danh mục sản phẩm.

Trang một danh mục sản phẩm.

- **Tệp giao diện:** `product/category.html`
- **Đường dẫn:** `/danh-muc/{slug} — hoặc đường dẫn riêng của danh mục`
- **Controller:** `App\Modules\Product\ProductController::categoryPageById`

Ngoài những biến dưới đây, slot này còn nhận toàn bộ [biến toàn cục](/docs/bien-toan-cuc/index.md).

## Biến của trang

| Biến | Kiểu | Mô tả | Từ phiên bản |
|---|---|---|---|
| `category` | [`Category`](/docs/doi-tuong/category/index.md) | Danh mục đang xem. | 1.0 |
| `breadcrumb` | [`Breadcrumb[]`](/docs/doi-tuong/breadcrumb/index.md) | Đường dẫn từ danh mục gốc tới danh mục hiện tại; url của mỗi mắt xích có thể null. | 1.0 |
| `subcategories` | [`Category[]`](/docs/doi-tuong/category/index.md) | Danh mục con trực tiếp. | 1.0 |
| `products` | [`Product[]`](/docs/doi-tuong/product/index.md) | Sản phẩm thuộc danh mục, của trang hiện tại. | 1.0 |
| `has_more` | `boolean` | Còn trang tiếp theo hay không. | 1.0 |
| `next_cursor` | `string` \| `null` | Con trỏ để tải trang kế; null khi đã hết. | 1.0 |
| `cursor` | `string` \| `null` | Con trỏ của trang đang xem. | 1.0 |
| `sort` | `string` \| `null` | Kiểu sắp xếp đang chọn. | 1.0 |
| `stock` | `string` \| `null` | Bộ lọc tồn kho đang chọn. | 1.0 |
| `warehouse_id` | `string` \| `null` | Kho đang lọc theo. | 1.0 |
| `warehouses` | [`Warehouse[]`](/docs/doi-tuong/warehouse/index.md) | Danh sách kho để dựng bộ lọc. | 1.0 |
| `category_content` | `string` \| `null` | Nội dung SEO dạng HTML đặt ở cuối trang; null khi danh mục chưa có. | 1.2 |
| `total_count` | `integer` | Tổng số sản phẩm khớp bộ lọc hiện tại (dùng cho tiêu đề "940 Laptop"). | 1.2 |
| `remaining_count` | `integer` | Số sản phẩm còn lại chưa hiển thị; dùng cho nút "Xem thêm N sản phẩm". | 1.2 |
| `filters` | [`CategoryFilterPanel`](/docs/doi-tuong/categoryfilterpanel/index.md) | Cấu hình bảng lọc của danh mục: hãng, nhóm thuộc tính và khoảng giá. | 1.2 |
| `active_filters` | `map` | Bộ lọc đang chọn, đã bỏ cursor — dùng để dựng liên kết bật/tắt từng lựa chọn. Rỗng khi không lọc gì. | 1.2 |
| `page_title` | `string` | Tiêu đề SEO của trang. | 1.0 |

## Mã đầy đủ

Toàn bộ tệp `product/category.html` của bộ giao diện mặc định. Chép về `templates/<bộ giao diện của bạn>/product/category.html` rồi sửa theo ý muốn.

```liquid
<div class="container page-content">

  {% if breadcrumb.size > 0 %}
  <nav class="breadcrumb" aria-label="breadcrumb">
    <a href="/">Trang chủ</a>
    {% for crumb in breadcrumb %}
      <span class="breadcrumb__sep">/</span>
      {% if forloop.last %}
        <span class="breadcrumb__current">{{ crumb.title | escape }}</span>
      {% elsif crumb.url %}
        <a href="{{ crumb.url | escape }}">{{ crumb.title | escape }}</a>
      {% else %}
        <span>{{ crumb.title | escape }}</span>
      {% endif %}
    {% endfor %}
  </nav>
  {% endif %}

  <div class="category-header">
    {% if category.cover_image_url %}
      <img src="{{ category.cover_image_url | escape }}" alt="{{ category.title | escape }}"
           class="category-header__img" loading="lazy">
    {% endif %}
    <h1 class="category-header__title">{{ category.title | escape }}</h1>
    {% if category.description %}
      <p class="category-header__desc">{{ category.description }}</p>
    {% endif %}
  </div>

  {% if subcategories.size > 0 %}
  <div class="subcategory-nav">
    {% for sub in subcategories %}
      {% if sub.url %}<a href="{{ sub.url | escape }}" class="subcategory-nav__item">{% else %}<span class="subcategory-nav__item">{% endif %}
        {% if sub.cover_image_url %}
          <img src="{{ sub.cover_image_url | escape }}" alt="{{ sub.title | escape }}"
               class="subcategory-nav__img" loading="lazy">
        {% endif %}
        <span>{{ sub.title | escape }}</span>
      {% if sub.url %}</a>{% else %}</span>{% endif %}
    {% endfor %}
  </div>
  {% endif %}

  <form method="get" class="storefront-sort">
    <label for="sort">Sắp xếp:</label>
    <select id="sort" name="sort" onchange="this.form.submit()">
      <option value=""           {% if sort == nil or sort == '' %}selected{% endif %}>Mặc định</option>
      <option value="price_asc"  {% if sort == 'price_asc' %}selected{% endif %}>Giá: Thấp đến cao</option>
      <option value="price_desc" {% if sort == 'price_desc' %}selected{% endif %}>Giá: Cao đến thấp</option>
    </select>
    {% if stock %}<input type="hidden" name="stock" value="{{ stock | escape }}">{% endif %}
    {% if warehouse_id %}<input type="hidden" name="warehouse_id" value="{{ warehouse_id | escape }}">{% endif %}
  </form>

  <form method="get" class="storefront-filters">
    {% if sort %}<input type="hidden" name="sort" value="{{ sort | escape }}">{% endif %}
    <label for="stock">Tồn kho:</label>
    <select id="stock" name="stock" onchange="this.form.submit()">
      <option value=""              {% if stock == nil or stock == '' %}selected{% endif %}>Tất cả</option>
      <option value="in_stock"      {% if stock == 'in_stock' %}selected{% endif %}>Còn hàng</option>
      <option value="out_of_stock"  {% if stock == 'out_of_stock' %}selected{% endif %}>Hết hàng</option>
    </select>

    {% if stock == 'in_stock' and warehouses.size > 0 %}
      <label for="warehouse_id">Tại kho:</label>
      <select id="warehouse_id" name="warehouse_id" onchange="this.form.submit()">
        <option value="">Tất cả kho</option>
        {% for w in warehouses %}
          <option value="{{ w.id | escape }}" {% if warehouse_id == w.id %}selected{% endif %}>{{ w.title | escape }}</option>
        {% endfor %}
      </select>
    {% endif %}
  </form>

  {% if products.size > 0 %}
    <div class="product-grid">
      {% for product in products %}
        {% include 'components/product_item' %}
      {% endfor %}
    </div>

    {% if has_more %}
    <div class="pagination">
      <a href="?{% if sort %}sort={{ sort | escape }}&{% endif %}{% if stock %}stock={{ stock | escape }}&{% endif %}{% if warehouse_id %}warehouse_id={{ warehouse_id | escape }}&{% endif %}cursor={{ next_cursor | url_encode }}" class="btn btn--outline">Xem thêm</a>
    </div>
    {% endif %}
  {% else %}
    <p class="empty-state">Danh mục này chưa có sản phẩm.</p>
  {% endif %}

</div>
```
