Checkout Blocks 中块的受支持 Liquid 语法
Checkout Blocks 支持部分 Liquid 语法,您可以在多种可用块类型中使用它来根据变量对结账自定义项进行个性化设置。
受支持的 Liquid 变量
以下列表包含可在动态内容块或订单项目内容块中使用的所有受支持 Liquid 变量。某些变量只能在结账的特定页面上访问,而其他变量只能包含在特定的块类型中。
有关每个变量的详细信息,请参阅 Shopify 的Liquid 对象文档。
结账 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 变量
订单项目变量只能在 订单项目内容块 内访问。
订单项目支持以下 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 }}显示整个结账 Liquid 内容
如果您需要查看 checkout 对象上存在哪些值,则可以使用 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" }}或者,您也可以将日期格式设置为“Feb 24, 2025”:
{% 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 -%}订单项目属性 (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 -%}