The metafield object
Metafields make it possible to store additional information for articles, blogs, collections, customers, orders, pages, products, the shop, and variants. You can access the metafield
object through the metafields
attribute of these resources.
Metafields support multiple data types, which determine the kind of information that's stored in the metafield
object. You can also apply Liquid metafield filters to this object in order to output metafield information in a standard format.
The metafield
object has the following attributes:
metafield.type
Anchor link to section titled "metafield.type"Returns the metafield type.
metafield.value
Anchor link to section titled "metafield.value"Returns the metafield value.
The following outlines the value format for each metafield type:
Type | Returned format |
---|---|
|
A string |
product_reference
|
A product object |
variant_reference
|
A variant object |
page_reference
|
A page object |
file_reference
|
|
|
A number |
|
A date object |
url_reference
|
A url string |
json
|
A JSON object |
boolean
|
A boolean |
color
|
A color object |
|
A measurement object |
rating
|
A rating object |
list
|
An array of objects, numbers, or strings, depending on the list type |
Access metafields
Anchor link to section titled "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:
For example, you might have the following metafield added to a product:
Type | Namespace | Key | Data |
---|---|---|---|
single_line_text_field |
instructions |
wash |
"Cold" |
Using this metafield information, the following input generates the following output:
Metafields with type json
will return a JSON object from .value
. For example, you may have the following metafield added to a product:
Type | Namespace | Key | Data |
---|---|---|---|
json |
my_fields |
category |
"{\"primary\":\"A\",\"alternatives\":[\"B\",\"C\"]}" |
Fields within JSON can be accessed via square brackets or dot:
JSON arrays can be looped over or accessed via index:
Direct access
Anchor link to section titled "Direct access"Metafields must be accessed directly using their associated key. For example, you can't loop over a namespace to get each individual metafield:
Deprecated metafields
Anchor link to section titled "Deprecated metafields"Deprecated metafields are older metafield types with limited functionality. The following metafield types are deprecated:
integer
json_string
string
These metafield types don't return the same metafield object attributes mentioned in the previous section. Instead, they return the metafield value directly.
The following outlines the returned value type for each deprecated metafield type:
Metafield type | Returned value type |
---|---|
integer
|
An integer |
json_string
|
A JSON object |
string
|
A string |
For example, you might have the following metafield added to a product:
Type | Namespace | Key | Data |
---|---|---|---|
string |
instructions |
wash |
"Cold" |
Using this metafield information, the following input generates the following output: