Checkout Blocks 區塊支援的 Liquid 語法
Checkout Blocks 支援部分 Liquid 語法,您可在多種可用的區塊類型中使用,依變數個人化結帳自訂內容。
支援的 Liquid 變數
以下清單包括所有可在動態內容區塊或商品項目內容區塊中使用的支援 Liquid 變數。部分變數僅在結帳的特定頁面可存取,另有些變數僅能用於特定區塊類型。
如需各變數的詳細資訊,請參閱 Shopify 的Liquid objects documentation。
結帳 Liquid 變數
以下為結帳支援的 Liquid 變數:
checkout.attributescheckout.currencycheckout.has_selling_plancheckout.item_countcheckout.line_items_subtotal_pricecheckout.localecheckout.marketcheckout.metafieldscheckout.notecheckout.requires_shippingcheckout.shipping_pricecheckout.tax_pricecheckout.total_price
顧客 Liquid 變數
以下為顧客支援的 Liquid 變數:
customer.idcustomer.b2bcustomer.full_namecustomer.first_namecustomer.last_namecustomer.emailcustomer.phone
本地化 Liquid 變數
以下為市場支援的 Liquid 變數:
localization.marketlocalization.market.idlocalization.market.handle
訂單 Liquid 變數
以下為訂單支援的 Liquid 變數:
checkout.order.idcheckout.order.legacyResourceIdcheckout.order.name
商店 Liquid 變數
以下為商店資訊支援的 Liquid 變數:
shop.nameshop.url
商品項目 Liquid 變數
商品項目變數只能在 Line item content block 中使用。
商品項目支援下列 Liquid 變數:
line_item.attributesline_item.gift_cardline_item.has_selling_planline_item.line_priceline_item.line_level_discount_allocationsline_item.line_level_total_discountline_item.options_with_valuesline_item.priceline_item.productline_item.product.is_gift_cardline_item.product.product_typeline_item.product.requires_selling_planline_item.product.tagsline_item.product.vendor
line_item.quantityline_item.requires_shippingline_item.skuline_item.subtitleline_item.titleline_item.triggerline_item.typeline_item.variantline_item.variant.available_for_saleline_item.variant.barcodeline_item.variant.compare_at_priceline_item.variant.idline_item.variant.priceline_item.variant.price.amountline_item.variant.price.currency_code
line_item.variant.requires_shippingline_item.variant.skuline_item.variant.titleline_item.variant.unit_priceline_item.variant.weightline_item.variant.weight_unit
line_item.variant_idline_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 -%}