metafield

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:

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

{% assign instructions = product.metafields.instructions %}
{% assign key = 'Wash' %}
<ul>
  <li>Wash: {{ instructions[key] }}</li>
  <li>Wash: {{ instructions['Wash'] }}</li>
  <li>Wash: {{ instructions.Wash }}</li>
</ul>

Output

Wash: Cold
Wash: Cold
Wash: Cold

You can use the following in product.liquid to output your second metafield:

{% assign instructions = product.metafields.instructions %}
{% assign key = 'Dry' %}
<ul>
  <li>Dry: {{ instructions[key] }}</li>
  <li>Dry: {{ instructions['Dry'] }}</li>
  <li>Dry: {{ instructions.Dry }}</li>
</ul>

If you need to output all metafields with the namespace instructions attached to a given product, use the following Liquid:

Input

<ul>
   {% for field in product.metafields.instructions %}
   <li>{{ field | first }}: {{ field | last }}</li>
   {% endfor %}
</ul>

Output

Wash: Cold
Dry: Tumble

The key of a metafield is {{ field | first }}, while the value is {{ field | last }}.

Want to discuss this page?

Visit the Shopify Community