Metafields store additional information on articles, blogs, collections, customers, orders, pages, products, the shop and variants. You access the metafield object through the metafields attribute of these resources.
Metafields support several data types that define what kind of information the metafield object holds. You can also apply metafield filters to output metafield data in a standard format.
Metafields of the types integer, json_string and string are older implementations. They do not have the attributes below and do not work with metafield filters. See Deprecated metafields.
The metafield object has the following attributes:
metafield.type
Returns the metafield type.
metafield.value
Returns the value of the metafield.
The value format for each metafield type:
| Type | Returned format |
|---|---|
single_line_text_field multi_line_text_field |
A string |
product_reference |
A product object |
variant_reference |
A variant object |
page_reference |
A page object |
file_reference |
A generic_file object A media object (images only) |
number_integer number_decimal |
A number |
date date_time |
A date object |
url_reference |
A url string |
json |
A JSON string |
boolean |
A boolean |
color |
A color object |
weight volume dimension |
A measurement object |
rating |
A rating object |
Access metafields
The access path for metafields has two levels:
namespace- a group of metafields that avoids conflicts.key- the name of the metafield.
Access the metafield object with this syntax:
{{ resource.metafields.namespace.key }}
For example, add this metafield to a product:
| Type | Namespace | Key | Data |
|---|---|---|---|
single_line_text_field |
instructions |
wash |
"Cold" |
With this metafield, the following input creates this output:
Type: {{ product.metafields.instructions.wash.type }}
Wash: {{ product.metafields.instructions.wash.value }}
Type: single_line_text_field
Wash: Cold
Direct access
Metafields must be accessed directly by their key. For example, you cannot loop over a namespace to get each metafield:
{% for field in resource.metafields.namespace %}
{{ field | metafield_tag }}
{% endfor %}
Deprecated metafields
Deprecated metafields are older metafield types with limited functionality. These metafield types are deprecated:
json_stringnumberstring
These types do not return the attributes of the metafield object described above. They return the value of the metafield directly.
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 |
Use a suitable metafield type for new metafields and consider updating deprecated types.
For example, add this metafield to a product:
| Type | Namespace | Key | Data |
|---|---|---|---|
string |
instructions |
wash |
"Cold" |
With this metafield, the following input creates this output:
Wash: {{ product.metafields.instructions.wash }}
Wash: Cold
More properties
VersaCommerce also provides these properties on the metafield object:
| Properties | Returns |
|---|---|
list? |
Whether this metafield represents a list |