Shopify专题
Liquid 对象
00 分钟
2022-10-18
2023-9-11
type
status
date
slug
summary
tags
category
icon
password

概述

Liquid 对象包含在页面上输出动态内容的属性。
Liquid 对象是主题主要数据来源。(其他方式如ajax也可以获取数据)
Liquid 中包括80多个对象。
Liquid 对象通常也称为 Liquid 变量。
Liquid 对象有三大类型:全局对象、内容对象、其他对象。
 
  • 全局对象
全局对象可以在主题的任何文件中使用。例如,所有页面都可以访问当前的页面标题:
  • 内容对象
内容对象用于输出模板和 section 文件的内容,以及由 shopify 加载的脚本和样式表。例如,在布局文件的body标签中使用 content_for_layout 对象输出模板的内容:
  • 其他对象
其他对象只有在特定的情况下使用,例如,可以在产品详情页使用 product 对象获取产品信息,比如输出产品标题:
 

全局对象

all_country_option_tags

all_country_option_tags 变量用于获取每个国家与其子区域数据。
all_country_option_tags 变量为每个国家输出一个<option>标签。
每个<option>标签都有一个data-province属性,该属性包含国家的子区域数组。
all_country_option_tags 需要被 <select> 标签包裹。
all_country_option_tags 对象应该被包装在<select>标签中:
输出:

country_option_tags

和 all_country_option_tags 类似,但只获取商店后台“发货和配送”页面“可发货区域”的国家或地区。

cart

cart 对象用于获取购物车数据。
cart 对象属性如下:
属性
说明
cart.attributes
用于购物车页面,通过 attributes[attribute-name] 来捕获更多信息
cart.cart_level_discount_applications
特定于购物车的 discount_application 对象数组
cart.currency
返回购物车的 currency 对象 如果你的商店使用多种货币,那么 currency 与客户的本地(显示)货币相同。 否则,currency 与你的商店货币相同。
cart.discount_applications
discount_application对象数组
cart.item_count
购物车中商品的数量
cart.items
line_item 对象数组
cart.items_subtotal_price
购物车的每一项打折后的价格的总和。 小计不包括税费(除非税费包含在价格中)、购物车折扣或运费。
cart.note
cart.note 允许在购物车页面上捕获更多信息
cart.original_total_price
折扣前购物车中所有商品的总价
cart.taxes_included
是否含税
cart.total_discount
购物车的所有折扣总额(节省的金额)
cart.total_price
折扣后购物车中所有商品的总价
cart.total_weight
购物车中所有商品的总重量

customer

customer 对象用于获取当前登录账户的信息。
如果用户未登录,customer 值为 nil。
如果在需要登录才能访问的页面,例如 /accounts 下属页面,则不需要判断是否未 nil,直接使用即可。
customer 对象属性如下:
属性
描述
customer.accepts_marketing
customer.addresses
与客户关联的所有地址组成的 customer_address 数组
customer.addresses_count
与客户关联的地址的数量
customer.default_address
默认的 customer_address
customer.email
客户的邮件
customer.first_name
客户的名字
customer.has_account
与客户关联的电子邮件是否绑定到客户帐户
customer.id
客户ID
customer.last_name
客户的姓
customer.last_order
客户最近下的订单,不包括测试订单
customer.name
客户的全名
customer.orders
客户下的订单 order 对象数组
customer.orders_count
客户已下的订单总数
customer.phone
客户的电话号码
customer.tags
与客户关联的标签数组,可能包含以下的值(或其他值):New Customer, wholesale, regular-customer, VIP
customer.tax_exempt
客户是否免税
customer.total_spent
客户在所有订单上花费的总金额

customer_address

customer_address 对象可通过 customer.addresses 获取。
customer_address 对象属性如下:
属性
描述
customer_address.address1
地址的 Address1 字段的值
customer_address.address2
地址的 Address2 字段的值
customer_address.city
地址的 City 字段的值
customer_address.company
地址的 Company 字段的值
customer_address.country
地址的 Country 字段的值
customer_address.country_code
ISO 3166-2标准格式地址的 Country 字段的值
customer_address.first_name
地址的 First Name 字段的值
customer_address.id
客户地址的id
customer_address.last_name
地址的 Last Name 字段的值
customer_address.phone
地址的 Phone 字段的值
customer_address.province
地址的 Province/State 字段的值
customer_address.province_code
地址的 Province/State 字段的缩写的值
customer_address.street
地址的 Address1 和 Address2 字段的组合值。
customer_address.zip
地址的 Postal/Zip 字段的值

内容对象

其他对象

?address

地址对象包含客户在Shopify的结帐页面中输入的信息。 请注意,客户可以输入两个地址:账单地址或送货地址。
访问地址对象的属性时,必须指定要访问的地址。 这可以通过在属性前使用shipping_address或billing_address来实现。
地址对象可以在电子邮件模板,结帐的订单状态页面,以及订单打印机等应用程序中使用。

article

article 对象用于获取某篇文章数据。
article 对象可在 article 页面获取。
article 属于某个 blog ,blog 相当于 article 的类别。
article对象属性如下:
属性
说明
article.author
文章作者全名
article.comments
文章评论的数组对象
article.comments_count
评论数量
article.comments_enabled
是否启用评论
article.comment_post_url
提交评论的地址
article.content
文章内容
article.created_at
文章创建时间
article.excerpt
文章摘录
article.excerpt_or_content
文章摘录或文章内容(如果没有摘录)
article.handle
文章的handle
article.id
文章ID
article.image
文章封面图 image 对象
article.image.alt
文章封面图 image 对象的alt
article.image.src
文章封面图 image 对象的src
article.moderated
是否要审核才能展示评论
article.published_at
文章发布时间
article.tags
文章的所有标签
tag.total_count
迭代 article.tags 时,打印tag展示标签名,打印tag.total_count展示有此标签的文章数量
article.title
文章标题
article.updated_at
文章更新时间
article.url
文章相对路径
article.user
文章作者 user 对象
article.user.account_owner
文章作者是否为商店的帐户所有者
article.user.bio
文章作者简介
article.user.email
文章作者email
article.user.first_name
文章作者名
article.user.homepage
文章作者主页
article.user.image
文章作者 image 对象
article.user.last_name
文章作者姓

block

block 对象用于获取 sections 的 block 数据。
block 对象可在 section 文件或 section 文件引入的 snippet 文件中获取。
通过循环 section.blocks 来获取 block 对象。
block 对象属性如下:
属性
说明
block.id
block 在 blocks 对象中的 key
block.settings
block settings 对象
block.shopify_attributes
表示块属性的字符串
block.type
block 的 type

blog

blog 对象用于获取博客数据。
blog 对象可在博客页面或文章页面获取。
博客是文章的类别,每篇文章都要属于某个博客。
博客可以统一设置其下的文章的评论规则(禁止评论、需审核评论、自动发布评论)。
blog 对象属性如下:
属性
说明
blog.all_tags
博客下所有文章的标签
blog.articles
博客下所有文章对象
blog.articles_count
博客下所有文章的数量(不包括隐藏的文章)
blog.comments_enabled
博客是否启用评论
blog.handle
博客的handle
blog.id
博客的id
blog.moderated
当前博客是否要审核才能展示评论
blog.next_article
下一篇文章对象(用于article页面)
blog.previous_article
上一篇文章对象(用于article页面)
blog.tags
博客下所有文章的标签。类似于all_tags,但只返回过滤视图中文章的标签
blog.title
博客标题
blog.url
博客相对路径

checkout

checkout 对象用于结账页。只有Shopify Plus商家才能自定义结账页。
checkout 对象属性可查阅官方文档:

collection

collection 对象用于获取产品系列数据(shopify 将 collection 翻译为 产品系列)。
collection 对象可在产品系列页面获取。
某个系列下可包含任意个产品。
collection 对象属性如下:
属性
描述
collection.all_products_count
返回系列中所有产品的数量,即便系列是经过筛选的
collection.all_tags
返回系列中所有产品标签的数量,即便系列是经过筛选的
collection.all_types
返回系列中所有产品的类别数组
collection.all_vendors
返回系列中所有产品的供应商数组
collection.current_type
当使用 collections/types 查询某种类型的产品,current_type 会返回 url 中的q查询参数的值
collection.current_vendor
当使用 collections/vendors 查询某个供应商的产品,current_vendor 会返回 url 中的q查询参数的值
collection.default_sort_by
返回系列的默认的产品排序,可以后台设置,有以下值:manual(手动)best-selling(畅销)title-ascending(产品标题A-Z)title-descending(产品标题Z-A)price-ascending(最低价格)price-descending(最高价格)created-ascending(时间旧->新)created-descending(时间新->旧)
collection.description
返回系列的描述
collection.featured_image
返回系列的图片,或第一个产品的图片
collection.filters
返回系列的筛选器对象数组
collection.handle
返回系列的handle
collection.id
返回系列的ID
collection.image
返回系列的图片
collection.next_product
返回系列中下一个产品的 product 对象,如果没有下一个产品,则返回nil
collection.previous_product
返回系列中上一个产品的 product 对象,如果没有上一个产品,则返回nil
collection.products
返回系列中所有产品的数组可以使用 paginate 标签来选择每页要显示的产品数量,最多不超过50个
collection.products_count
返回集合中与当前视图匹配的产品数量
collection.published_at
返回系列的发布时间
collection.sort_by
返回 url 中的 sort_by 查询参数的值
collection.sort_options
返回系列排序选项数组,每一个选项包含两个属性:option.name, option.value
collection.template_suffix
返回系列的自定义模板后缀。例如,如果有一个系列模板:collection.no-price.liquid,则返回 no-price
collection.title
返回系列的标题
collection.tags
返回当前应用于筛选后的系列的标签,或nil
collection.url
返回系列的url

color

color 对象用于颜色的详细信息,比如rgba,hsl。直接打印此对象会得到颜色值。
color 对象从 color 类型的 settings 获取。
color 对象属性如下:
属性
描述
color.alpha
颜色的透明度
color.blue
颜色的蓝色值
color.green
颜色的绿色值
color.hue
颜色的色相
color.lightness
颜色的亮度
color.red
颜色的红色值
color.saturation
颜色的饱和度

comment

comment 对象用于获取某篇文章的评论数据。
comment 对象可通过遍历 article.comments 获取。
comment 对象属性如下:
属性
描述
comment.author
评论的作者
comment.content
评论内容
comment.created_at
评论创建时间
comment.email
评论的email
comment.id
评论ID
comment.status
评论的状态。有以下状态:pending : 待处理published : 已发布removed : 已删除spam : 垃圾评论unapproved : 未批准
comment.updated_at
评论更新时间
comment.url
评论url(文章url加上一个hash,用于滚动到页面指定位置)

?country

currency

currency 对象用于获取货币的信息(符号、名称等)。
currency 对象可通过 shop.currency、cart.currency、checkout.currency 等对象获取。
currency 对象属性如下:
属性
描述
currency.name
货币名称
currency.iso_code
货币ISO编码
currency.symbol
货币符号

current_page

current_page 变量用于获取浏览分页内容时的页码,一般用在 paginate 标签内,也可以用在外面。

?current_tags