Checkout Blocks 區塊支援的 Liquid 語法

Checkout Blocks 支援 Liquid 語法子集,您可以在一些可用區塊類型使用這些語法來根據變數對結帳頁面自訂內容進行個人化設定。

支援的 Liquid 變數

以下清單包含所有支援的 Liquid 變數,這些變數可用於動態內容區塊或商品項目內容區塊。某些變數只能在某些結帳頁面存取,而其他變數只能包含在特定區塊類型。

有關每個變數的特定內容,請參閱 Shopify Liquid 物件文件

結帳頁面 Liquid 變數

以下 Liquid 變數適用於結帳頁面:

  • checkout.attributes
  • checkout.currency
  • checkout.has_selling_plan
  • checkout.item_count
  • checkout.line_items_subtotal_price
  • checkout.locale
  • checkout.market
  • checkout.metafields
  • checkout.note
  • checkout.requires_shipping
  • checkout.shipping_price
  • checkout.tax_price
  • checkout.total_price

顧客 Liquid 變數

以下 Liquid 變數適用於顧客:

  • customer.id
  • customer.b2b
  • customer.full_name
  • customer.first_name
  • customer.last_name
  • customer.email
  • customer.phone

本地化 Liquid 變數

以下 Liquid 變數適用於市場:

  • localization.market
    • localization.market.id
    • localization.market.handle

訂單 Liquid 變數

以下 Liquid 變數適用於訂單:

  • checkout.order.id
  • checkout.order.legacyResourceId
  • checkout.order.name

Shop Liquid 變數

以下 Liquid 變數適用於商店資訊:

  • shop.name
  • shop.url

商品項目 Liquid 變數

只能在商品項目內容」區塊中存取商品項目變數。

以下 Liquid 變數適用於商品項目:

  • line_item.attributes
  • line_item.gift_card
  • line_item.has_selling_plan
  • line_item.line_price
  • line_item.line_level_discount_allocations
  • line_item.line_level_total_discount
  • line_item.options_with_values
  • line_item.price
  • line_item.product

    • line_item.product.is_gift_card
    • line_item.product.product_type
    • line_item.product.requires_selling_plan
    • line_item.product.tags
    • line_item.product.vendor
  • line_item.quantity

  • line_item.requires_shipping

  • line_item.sku

  • line_item.subtitle

  • line_item.title

  • line_item.trigger

  • line_item.type

  • line_item.variant

    • line_item.variant.available_for_sale
    • line_item.variant.barcode
    • line_item.variant.compare_at_price
    • line_item.variant.id
    • line_item.variant.price
      • line_item.variant.price.amount
      • line_item.variant.price.currency_code
    • line_item.variant.requires_shipping
    • line_item.variant.sku
    • line_item.variant.title
    • line_item.variant.unit_price
    • line_item.variant.weight
    • line_item.variant.weight_unit
  • line_item.variant_id

  • line_item.vendor

使用 Liquid 的程式碼片段範例

以下是 Checkout Blocks 支援的一些 Liquid 程式碼片段範例。

檢查結帳頁面是否為 B2B

只有當結帳頁面為 B2B 時,才能顯示內容。

{%- if customer.b2b -%}
B2B
{%- endif -%}

剖析 JSON

您可以剖析 JSON 值,例如來自商品項目屬性(性質)或中繼欄位的值。

{%- assign complex_json = checkout.metafields.checkoutblocks.complex | json -%}

設定幣別格式

Checkout Blocks 完全支援多種幣別結帳頁面。只需透過貨幣篩選條件,即可使用啟用的幣別格式來剖析貨幣並設定其格式。這不會自動轉換貨幣。

{{ checkout.total_price | money }}

購物車備註

您可以顯示購物車備註的值,例如購物車中輸入的值。

{{ checkout.note }}

購物車屬性

若要顯示特定購物車屬性的值(例如配送日期),您可以使用此程式碼片段。請務必變更配送日期鍵值, 以匹配您自己的日期。

{% assign delivery_date = '' %}
{% for attribute in checkout.attributes %}
  {% if attribute.key == 'Delivery date' %}
    {% assign delivery_date = attribute.value %}
  {% endif %}
{% endfor %}

Delivery Date: {{ delivery_date }}

顯示整個結帳頁面 Liquid 內容

如果您需要查看結帳頁面物件上存在的值,可以使用 JSON 篩選條件進行序列化。這應僅用於除錯目的。

{{ checkout | json }}

結帳頁面中繼欄位

您可以透過引用結帳頁面中繼欄位,在感謝和訂單狀態頁面顯示儲存到結帳頁面的自訂欄位。深入瞭解如何存取中繼欄位

your-namespace 替換為您的命名空間,將 your-custom-field-key 替換為區塊中定義的自訂欄位鍵值

{{ checkout.metafields.your-namespace.your-custom-field-key.value }}

設定日期格式

在此範例,我們建立了 4 天後的日期(432000 秒),然後進行格式設定。

您可以傳回格式為「02/24/2025」的日期:

{% assign future_date = "now" | date: "%s" | plus: 432000 %}
{{ future_date | date: "%m/%d/%Y" }}

或者,您可以將日期格式化為「2025 年 2 月 24 日」:

{% assign future_date = "now" | date: "%s" | plus: 432000 %}
{{ future_date | date: "%b %d, %Y" }}

深入瞭解 Liquid 的日期格式設定

商品項目內容自訂

以下是一些可用於商品項目內容區塊的 Liquid 程式碼片段範例。

條件式顯示比較售價

您可以使用此程式碼片段條件式顯示商品項目的比較售價。

{%- if line_item.variant.compare_at_price.amount -%}
On sale. Originally {{ line_item.variant.compare_at_price.amount | times: line_item.quantity | money }}
{%- endif -%}

根據產品標籤顯示內容

您可以根據區分大小寫的產品標籤條件式顯示商品項目內容。

{%- if line_item.product.tags contains 'final-sale' -%}
Final sale
{%- endif -%}

商品項目屬性(性質)

您可以迭代商品項目屬性(也稱為商品項目性質),並顯示配送估計時間、預購等資訊。

{%- assign first_line_attribute = line_item.attributes | first -%}
{%- assign first_attribute_value = first_line_attribute.value | json_parse -%}
{%- assign message = first_attribute_value.message -%}
{%- if message -%}
  {{ message }}
{%- endif -%}

顯示「中繼欄位觸發條件」值

以下程式碼將包含觸發條件值。

{{ line_item.trigger }}

顯示商品項目的定期總額

如果您需要在一次性折扣之前顯示訂閱商品項目的定期總額,您可使用 line_level_total_discount 值。

{%- if line_item.line_level_total_discount > 0 and line_item.has_selling_plan -%}
Recurring total: {{ line_item.line_price | plus: line_item.line_level_total_discount | money }}
{%- endif -%}
沒有找到您需要的答案嗎?我們很樂意為您提供協助。