Liquid Tutorial

Metafields in Liquid

Source: Coding with Robby

Custom Data Beyond Standard Fields

Metafields extend Shopify resources with merchant- or app-defined data: course duration, instructor bio, video URLs, sizing charts. They are organized by namespace and key, accessed as resource.metafields.namespace.key.

Product metafields for a course store
{% if product.metafields.custom.duration != blank %}
  <p class="meta">Duration: {{ product.metafields.custom.duration }}</p>
{% endif %}

{% if product.metafields.custom.instructor != blank %}
  <p>Taught by {{ product.metafields.custom.instructor }}</p>
{% endif %}

{% if product.metafields.custom.preview_video %}
  <video controls poster="{{ product.featured_image | image_url: width: 800 }}">
    <source src="{{ product.metafields.custom.preview_video | file_url }}">
  </video>
{% endif %}
  1. Define metafields in Admin → Settings → Custom data, or via an app
  2. Namespace 'custom' is the default for merchant-created fields
  3. Use | metafield_tag for Shopify's typed rendering (rich text, colors, references)
  4. Use | file_url for file_reference metafields (videos, PDFs)
  5. Always check != blank before rendering — undefined metafields return nil

List and JSON Metafields

Iterate a list metafield definition
{% for module in product.metafields.custom.curriculum.value %}
  <li>{{ module.title }} — {{ module.lessons }} lessons</li>
{% endfor %}
Tip: Metafield definitions include a type (single_line_text, number_integer, list.metaobject_reference). Access .value on metaobject and list types for the parsed data.