Checkout Blocks 區塊支援的 Liquid 語法

Checkout Blocks 支援部分 Liquid 語法,您可在多種可用的區塊類型中使用,依變數個人化結帳自訂內容。

支援的 Liquid 變數

以下清單包括所有可在動態內容區塊或商品項目內容區塊中使用的支援 Liquid 變數。部分變數僅在結帳的特定頁面可存取,另有些變數僅能用於特定區塊類型。

如需各變數的詳細資訊,請參閱 Shopify 的Liquid objects documentation

結帳 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

商店 Liquid 變數

以下為商店資訊支援的 Liquid 變數:

  • shop.name
  • shop.url

商品項目 Liquid 變數

商品項目變數只能在 Line item content block 中使用。

商品項目支援下列 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 值,例如來自商品項目屬性 (properties) 或中繼欄位的值。

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

格式化幣別

Checkout Blocks 完整支援多種幣別結帳。只要套用 money 篩選器,即可依目前的幣別格式解析並格式化金額。這不會自動轉換幣別。

{{ checkout.total_price | money }}

購物車備註

您可以顯示購物車備註的內容,例如購物車中輸入的文字。

{{ checkout.note }}

購物車屬性

若要顯示特定購物車屬性的值 (例如配送日期),可使用此程式碼片段。請務必將鍵名 Delivery date 改為您自己的鍵名。

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

Delivery Date: {{ delivery_date }}

顯示 checkout 物件的完整內容

若需要檢視 checkout 物件中有哪些值,可使用 JSON 篩選器將其序列化。此方式僅供偵錯使用。

{{ checkout | json }}

結帳中繼欄位

您可以在感謝頁面與訂單狀態頁面透過參照結帳中繼欄位,顯示儲存到結帳的自訂欄位。瞭解詳情accessing metafields

請將 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" }}

或者,您也可以將日期格式化為「Feb 24, 2025」:

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

瞭解詳情formatting dates in Liquid

商品項目內容自訂

以下是一些可用於 Line item content blocks 的 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 -%}

商品項目屬性 (properties)

您可以逐一列出商品項目屬性 (亦稱為 line item properties),並顯示配送預估、預購等資訊。

{%- 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 -%}

顯示「metafield trigger」值

以下程式碼會包含該觸發條件的值。

{{ 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 -%}