2.9 KiB
layout, title, nav
| layout | title | nav | ||
|---|---|---|---|---|
| default | metafield |
|
metafields
The metafields object allows you to store additional information for products, collections, orders, blogs, pages and your shop. You can output metafields on your storefront using Liquid.
There are several Shopify apps and browser add-ons that make use of the Shopify API to let you manage your metafields:
- Shopify FD to create and edit metafields
- Custom Fields to edit your metafields
- Metafields Editor
- Metafields2
A metafield consists of a namespace, a key, a value, and a description (optional). Use the namespace to group different metafields together in a logical way.
You can also specify metafields as either integers or strings. That way, you’ll end up with the right type of data when you use the metafields in your Liquid.
For example, if you’ve added two metafields to a product, and each metafield has the following attributes:
| Namespace | Key | Value |
|---|---|---|
| instructions | Wash | Cold |
| instructions | Dry | Tumble |
You can then use the following Liquid in product.liquid to output your metafield:
Input
{% highlight html %} {% raw %} {% assign instructions = product.metafields.instructions %} {% assign key = 'Wash' %}- Wash: {{ instructions[key] }}
- Wash: {{ instructions['Wash'] }}
- Wash: {{ instructions.Wash }}
Output
{% highlight html %} Wash: Cold Wash: Cold Wash: Cold {% endhighlight %}You can use the following in product.liquid to output your second metafield:
{% highlight html %} {% raw %} {% assign instructions = product.metafields.instructions %} {% assign key = 'Dry' %}
- Dry: {{ instructions[key] }}
- Dry: {{ instructions['Dry'] }}
- Dry: {{ instructions.Dry }}
If you need to output all metafields with the namespace instructions attached to a given product, use the following Liquid:
Input
{% highlight html %} {% raw %}-
{% for field in product.metafields.instructions %}
- {{ field | first }}: {{ field | last }} {% endfor %}
Output
{% highlight html %} Wash: Cold Dry: Tumble {% endhighlight %}The key of a metafield is {% raw %}{{ field | first }}{% endraw %}, while the value is {% raw %}{{ field | last }}{% endraw %}.