# components/product_item

> Thẻ sản phẩm dùng trong mọi lưới sản phẩm: ảnh, tên, giá gạch ngang và nút thêm vào giỏ.

Thẻ sản phẩm dùng trong mọi lưới sản phẩm: ảnh, tên, giá gạch ngang và nút thêm vào giỏ.

- **Tệp giao diện:** `components/product_item.html`

## Biến đầu vào

Component đọc biến có sẵn tại chỗ chèn. Những biến dưới đây phải tồn tại, nếu không component sẽ dựng ra khối rỗng.

| Biến | Kiểu | Bắt buộc | Mô tả |
|---|---|---|---|
| `product` | [`Product`](/docs/doi-tuong/product/index.md) | có | Sản phẩm cần hiển thị; luôn lấy từ biến vòng lặp {% for product in ... %}. |

## Cách chèn

```liquid
{% for product in featured_products %}
  {% include 'components/product_item' %}
{% endfor %}
```

Đường dẫn trong `{% include %}` tính từ gốc bộ giao diện và **không có đuôi `.html`**.

## Mã đầy đủ

Toàn bộ tệp `components/product_item.html` của bộ giao diện mặc định.

```liquid
{% comment %}
  Thẻ sản phẩm dùng chung cho trang danh mục, tìm kiếm và các section.
  Sản phẩm dùng đường dẫn riêng của chính nó: {{ product.url | escape }}.
  Khi product.url trống (sản phẩm chưa có đường dẫn), hiển thị dạng chữ
  thay vì tạo liên kết hỏng.
  Tuỳ chọn: card_eager = true để ảnh không lazy-load (thẻ đầu màn hình).
{% endcomment %}
{%- assign pc_second = nil -%}
{%- if settings.show_second_image != false -%}
  {%- if product.images.size > 1 -%}{%- assign pc_second = product.images[1].url -%}{%- endif -%}
{%- endif -%}
{%- assign pc_brand = product.brand.title -%}
<div class="product-card{% if pc_second %} product-card--has-second{% endif %}">
  {% if product.url %}<a href="{{ product.url | escape }}" class="product-card__img-link" tabindex="-1" aria-hidden="true">{% else %}<span class="product-card__img-link">{% endif %}
    {% if product.cover_image_url %}
      <img src="{{ product.cover_image_url | escape }}" alt="{{ product.title | escape }}"
           class="product-card__img" width="600" height="600" {% if card_eager %}fetchpriority="high"{% else %}loading="lazy"{% endif %} decoding="async">
      {% if pc_second %}<img src="{{ pc_second | escape }}" alt="" class="product-card__img product-card__img--second" width="600" height="600" loading="lazy" decoding="async">{% endif %}
    {% else %}
      <span class="product-card__img-placeholder">{{ 'product-1' | placeholder_svg: 'placeholder-svg' }}</span>
    {% endif %}
    {% if product.compare_at_price and product.compare_at_price > product.price %}
      <span class="product-card__badge">Giảm giá</span>
    {% elsif product.compare_at_price %}
      <span class="product-card__badge">Sale</span>
    {% endif %}
  {% if product.url %}</a>{% else %}</span>{% endif %}
  <div class="product-card__body">
    {% if settings.show_vendor != false and pc_brand %}<p class="product-card__vendor">{{ pc_brand | escape }}</p>{% endif %}
    {% if product.url %}
      <a href="{{ product.url | escape }}" class="product-card__name">{{ product.title | escape }}</a>
    {% else %}
      <span class="product-card__name">{{ product.title | escape }}</span>
    {% endif %}
    <div class="product-card__price">
      {% if product.compare_at_price %}
        <span class="price price--sale">{{ product.price | default: 0 | money }}</span>
        <span class="price price--original"><span class="visually-hidden">Giá gốc </span>{{ product.compare_at_price | money }}</span>
      {% else %}
        <span class="price">{{ product.price | default: 0 | money }}</span>
      {% endif %}
    </div>
    <button type="button" class="btn btn--outline btn--sm btn--block quick-add"
            data-product-slug="{{ product.slug | escape }}"{% if product.url %} data-product-url="{{ product.url | escape }}"{% endif %}{% if product.default_variant_id %} data-variant-id="{{ product.default_variant_id | escape }}"{% endif %}>
      Thêm vào giỏ<span class="visually-hidden">: {{ product.title | escape }}</span>
    </button>
  </div>
</div>
```

## Dùng ở

| Trang |
|---|
| [`home/index`](/docs/bien-theo-trang/home-index/index.md) |
| [`product/index`](/docs/bien-theo-trang/product-index/index.md) |
| [`product/detail`](/docs/bien-theo-trang/product-detail/index.md) |
| [`product/category`](/docs/bien-theo-trang/product-category/index.md) |
| [`product/collection-detail`](/docs/bien-theo-trang/product-collection-detail/index.md) |
| [`product/search`](/docs/bien-theo-trang/product-search/index.md) |

## Trường được dùng

Component chỉ đọc những trường này của [`Product`](/docs/doi-tuong/product/index.md). Sửa component để đọc thêm trường khác thì nhớ cập nhật `catalog/components.yaml`.

- `product.url`
- `product.cover_image_url`
- `product.title`
- `product.price`
- `product.compare_at_price`
- `product.slug`
- `product.images`
- `product.brand`
- `product.default_variant_id`
