metafield
A metafield attached to a parent object.
To learn about how to access a metafield on a specific object, refer to Access metafields.
Metafields support multiple data types, which determine the kind of information that's stored in the metafield. You can also output the metafield content in a type-specific format using metafield filters.
You can't create metafields in Liquid. Metafields can be created in only the following ways:
Metafields of type integer, , and string are older implementations that don't have the properties
noted on this page, and aren't compatible with metafield filters. To learn more, refer to Deprecated metafields.
Properties
Returns
trueif the metafield is a list type. Returnsfalseif not.TipTo learn about metafield types, refer to Metafield types.
The type of the metafield.
Possible values single_line_text_field multi_line_text_field rich_text_field product_reference collection_reference variant_reference page_reference file_reference number_integer number_decimal date date_time url_reference json boolean color weight volume dimension rating money - value
The value of the metafield.
The following table outlines the value format for each metafield type:
Type Returned format single_line_text_field
multi_line_text_fieldA string rich_text_fieldA field that supports headings, lists, links, bold, and italics product_referenceA product object collection_referenceA collection object variant_referenceA variant object page_referenceA page object file_referenceA generic_file object
A media object (images and videos only)number_integer
number_decimalA number date
date_timeA date string. To format the string, use the date filter. url_referenceA url string jsonA JSON object booleanA boolean colorA color object weight
volume
dimensionA measurement object ratingA rating object moneyA money object, displayed in the customer's local (presentment) currency.
Example
{
"list?": false,
"type": "single_line_text_field",
"value": "Take with a meal."
}Access metafields
The access path for metafields consists of two layers:
- namespace - A grouping of metafields to prevent conflicts.
- key - The metafield name.
Given this, you can access the metafield object with the following syntax:
Code
Type: {{ product.metafields.information.directions.type }}
Value: {{ product.metafields.information.directions.value }}Data
{
"product": {
"metafields": {}
}
}Output
Accessing metafields of type json
jsonThe value property of metafields of type json returns a JSON object. You can access the properties of this object directly in Liquid, either by name or 0-based index. You can also iterate through the properties.
Code
Temperature: {{ product.metafields.information.burn_temperature.value.temperature }}
Unit: {{ product.metafields.information.burn_temperature.value['unit'] }}
{% for property in product.metafields.information.burn_temperature.value -%}
{{ property.first | capitalize }}: {{ property.last }}
{%- endfor %}Data
{
"product": {
"metafields": {}
}
}Output
Accessing metafields of type list
listThe value property of metafields of type list returns an array. You can iterate through the array to access the values.
Code
{% for item in product.metafields.information.combine_with.value -%}
{{ item.product.title }}
{%- endfor %}Data
{
"product": {
"metafields": {}
}
}Output
If the list is of type , then you can access the items in the array directly in Liquid using a 0-based index.
Code
First item in list: {{ product.metafields.information.pickup_locations.value[0] }}
Last item in list: {{ product.metafields.information.pickup_locations.value.last }}Data
{
"product": {
"metafields": {}
}
}Output
Determining the length of a list metafield
The way that you determine the length of a list metafield depends on its type:
- Reference types: Use the
countproperty to determine the list length. - Non-reference types: These lists are rendered as arrays. Use the
sizefilter to determine the number of items in the array.
Code
# list.product_reference
Number of similar products: {{ product.metafields.information.similar_products.value.count }}
# list.single_line_text_field
Number of pickup locations: {{ product.metafields.information.pickup_locations.value.size }}Data
{
"product": {
"metafields": {}
}
}Output
Deprecated metafields
Deprecated metafields are older metafield types with limited functionality. The following metafield types are deprecated:
integerstring
These metafield types don't have the same metafield object properties mentioned in the previous sections. Instead, they return the metafield value directly.
The following table outlines the value type for each deprecated metafield type:
| Metafield type | Value type |
|---|---|
integer | An integer |
| A JSON object |
string | A string |