The metafield object

The metafields object allows you to store additional information for products, collections, customers, orders, blogs, pages and your shop. You can output metafields on your storefront using Liquid.

There are several apps in the Shopify App Store that you can use to 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.

Metafields are specified with the value_type as one of the following:

  • integer
  • string
  • json_string

By using the correct value_type, 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 }}.

For metafields with a value_type of 'json_string', the metafield is converted to an iterable hash or array:

Input

Storage instructions:
<ul>
  {% for key_value in product.metafields.instructions['storage'] %}
    <li>{{ key_value[0] }}: {{ key_value[1] }}</li>
  {% endfor %}
</ul>

Output

Storage instructions:
min_temp: 0
max_temp: 50

Examples

You can use metafields to add extra content to a product page.

For example, you could add tabs to a product page and add links to related products:

{%- assign tabs = product.metafields.tabs -%}
<ul class="tabs">
    {% include 'product-description' %}
    {%- for field in tabs -%}
        <li><h2>{{ field | first }}</h2>{{ field | last }}</li>
    {%- endfor -%}
    {% include 'related-products' %}
</ul>

For example, you could check if a product has washing instructions and show the instructions:

{% unless product.metafields.Acme134-instructions.Wash == blank %}
Wash: {{ product.metafields.Acme134-instructions.Wash }}
{% endunless %}

You can use metafields to store attributes about a product. For example, you could create a metafield to store the color of a product and then set the background of the product page to the same color.

{%- if product.metafields.style.colour != blank -%}
  {%- assign mColour = product.metafields.style.colour -%}
  <style>
      .add-to-cart {
        background: {{ mColour }}
      }
  </style>
{%- endif -%}

You can use metafields to keep track of your inventory. For example you could use this information to show delivery timeframes, backorder dates, and units sold on a checkout page.

{% if backorder != blank %}<div>This item is backordered, and will ship in {{ backorder }}</div>{% endif %}

<div class="stock-sold-bar">
<span data-start="{{ startCount }}" data-stock="{{ currentCount }}" data-percent="{{ soldPercent }}"></span>
</div>

You can use metafields to track your inventory across multiple locations. For example, you could use this information to help track where your products are located:

{%- assign locations = product.metafields.locations -%}
{%- assign items = locations | split: ';' -%}
<ul class="store-location">
    {%- for item in items -%}
        {% assign stock = item | split:',' %}
        <li>{{ stock | first }}: {{ stock | last }}</li>
    {%- endfor -%}
</ul>

You can use metafields to group related products:

{%- assign r = product.metafields.related.items | split:"," -%}
<div class="related-products">
    {%- for p in r -%}
        {% assign relatedProduct = all_products[p] %}
        {%- include 'some-snippet' product: relatedProduct -%}
    {%- endfor -%}
</div>

You can use metafields to nest products:

{%- assign cParent = product.metafields.col.parent -%}
{%- assign cChild = product.metafields.col.child -%}
<div>
    {%- if collections[cChild].products.size > 0 -%}
       {%- include 'collection-loop' with collections[cChild] -%}
    {%- endif -%}
</div>

You can use metafields to store SEO data, such as custom URLS and meta tags. For example, to add noindex and nofollow meta tags to an product page, create a metafield for the object with the following attributes:

"namespace" : "seo"
"key" : "hidden"
"value" : 1
"value_type" : "integer"

Learn more

Ready to start selling with Shopify?

Try it free