Start building

Guides

How to use Shopify metafields in a section, and why yours might print the wrong thing

Start from what you're seeing on the page. Most metafield problems come down to the type, the template or the blank check, and each has a one-line fix.

The Klorr teamUpdated 8 min read

  • Metafields
  • Metaobjects
  • Liquid
  • Dynamic sources

Key takeaways

  • Every metafield has a type, and the type decides what .value gives you: a string, a number, a product object or a list. Most bugs are a mismatch here.
  • Connect a setting to a dynamic source when one value fills one field. Read the metafield directly in Liquid when the section must loop over a list or make a decision.
  • Rich text needs metafield_tag, since printing .value doesn't give you formatted HTML.
  • false counts as blank in Liquid, so a true-or-false metafield set to false looks like a missing one unless you compare it to true.

You added {{ product.metafields.custom.care.value }} to a section, saved, and the page shows a wall of JSON. Or nothing. Or the word ProductDrop. Each of those points to a specific cause, so find yours in this list first.

What you're seeing, and the fix

On the pageCauseFix
Nothing at all, on the home pageproduct doesn't exist on that templateUse a product setting: section.settings.product.metafields…
Nothing at all, on a product pageWrong namespace or key, or no value on this productCopy the name from the definition in the admin. It's usually custom.something
Text like {"type":"root","children":…A rich text field printed directly{{ field | metafield_tag }}
An object name or a bare path where a picture should beA file reference printed directly{{ field.value | image_url: width: 800 | image_tag }}
ProductDrop or a similar wordA reference printed directlyRead a property: field.value.title
A number with no unit, or {"value":…}A measurement or money valueUse metafield_text, .value.value with .value.unit, or | money
A false value is treated as emptyfalse counts as blankCompare: field.value == true, and check existence separately
The wrong thing from a key named sizesize, first and last act as built-in filtersUse brackets: product.metafields.custom["size"]
Value shows in the editor and not in Liquid, or the reverseA setting's dynamic source and your hard-coded key point at different metafieldsDecide on one. See below

The basic read

section.liquid
{%- comment -%} Namespace, then key. Metafields can't be created in Liquid, only read. {%- endcomment -%}
{{ product.metafields.custom.material }}          {%- comment -%} the value, as text {%- endcomment -%}
{{ product.metafields.custom.material.value }}    {%- comment -%} the value, typed {%- endcomment -%}
{{ product.metafields.custom.material.type }}     {%- comment -%} e.g. single_line_text_field {%- endcomment -%}
{{ product.metafields.custom.material.list? }}    {%- comment -%} true for list types {%- endcomment -%}

For plain text, printing with or without .value looks the same, which is why the habit of dropping it goes unnoticed until you hit another type. As a rule: print .value when you want a property of what it holds, use the `metafield_tag` filter on the field itself when you want Shopify to build the HTML, and don't guess in between. Resources other than products work the same way: collection.metafields, variant.metafields, shop.metafields, page.metafields.

What each type gives you

Type.value isHow to print it
single_line_text_field, multi_line_text_fieldA string{{ f.value | escape }}, adding newline_to_br for multi-line
rich_text_fieldStructured content, not HTML{{ f | metafield_tag }}
number_integer, number_decimalA number{{ f.value }}, and it works in plus or if
booleantrue or false{% if f.value == true %}
date, date_timeA date string{{ f.value | date: '%b %d, %Y' }}
urlA URL string<a href="{{ f.value }}">
colorA color object{{ f.value }}, or | color_to_rgb and similar
weight, volume, dimensionA measurement object{{ f.value.value }} {{ f.value.unit }}, or {{ f | metafield_text }}
ratingA rating object{{ f.value.rating }} / {{ f.value.scale_max }}
moneyA money object, in the shopper's currency{{ f.value | money }}
jsonAn object{{ f.value.key }}, or loop over it
file_referenceA media object for images and videos, otherwise a generic file{{ f.value | image_url: width: 800 | image_tag }}
product_reference and other referencesThat product, collection, variant or page{{ f.value.title }}, {{ f.value.url }}
list.*An array{% for item in f.value %}

Counting is the one place lists split: for reference lists, use f.value.count, and for other lists, use f.value | size. That and the full list are in Shopify's metafield reference. metafield_tag doesn't support lists other than list.single_line_text_field and list.metaobject_reference, so loop over the rest yourself.

Dawn's own product card is a good model for a typed read. It checks card_product.metafields.reviews.rating.value != blank before printing anything, then reads .rating and .scale_max from the value. The two checks you'll want most often are that blank test and field.value == true for booleans, and neither fails noisily when you get them wrong.

Dynamic source, or read it in Liquid?

There are two ways to get a metafield into a section. With a dynamic source, the section has an ordinary setting, and the merchant clicks the connect icon beside it and picks the metafield. The code just prints section.settings.text, with no idea where the value came from. With a direct read, your Liquid names the metafield itself.

Dynamic sourceDirect read in Liquid
Who chooses the metafieldThe merchant, in the editorYou, in code (or a text setting)
Falls back to typed text on other templatesYes, the setting can hold eitherYou write the fallback
Loop over a list and output a card for eachNo, one setting gets one valueYes
Make a decision ("if waterproof, show this badge")Not on its ownYes
Works in a section reused across themesYes, nothing is hard-codedOnly if the same metafield exists
Limits100 per template or group, 50 per setting or static sectionNone of those

Default to a dynamic source. It makes the section reusable and hands the choice to the person who knows the data, and it needs no code at all. Reach for a direct read when the section needs a repeat or a condition. Not every setting can connect to every type, though. Shopify's table is the reference, and these are the pairings people look for most:

Setting typeConnects to
text, inline_richtextSingle-line text, numbers, dates, measurements, rating, money, and a list of single-line text
richtextAll of the above, plus multi-line text, rich text and links
image_pickerfile_reference
videofile_reference that accepts video
product, collection, pageThe matching reference
product_list, collection_listA list of the matching reference
colorcolor
urlurl
metaobject, metaobject_listA reference to the same metaobject type

A setting's default can name a dynamic source too, but only as a direct reference, and only where that value exists. "default": "{{ product.title }}" is allowed in a section limited to product templates, while "Featuring: {{ product.title }}" gives an error because of the extra words.

A section that has to read directly

A skincare shop wants an ingredient list on each product, where each ingredient has a name, an amount, a note and a picture, and several products share ingredients. One setting can't output a repeating card, so this is a job for a direct read. The data belongs in metaobjects, so one ingredient is edited once:

  1. Create the metaobject

    In Settings › Metafields and metaobjects, add a definition called Ingredient with fields name (single-line text), amount (single-line text), note (multi-line text) and photo (file). Turn on storefront access for it.

  2. Create the product metafield

    Under product metafields, add custom.ingredients, type Metaobject, reference Ingredient, and choose List of entries.

  3. Fill in a product

    Create some ingredient entries and pick them on the product. Check that any publishing status is set to active, since a draft entry returns nil.

  4. Add the section

    Follow the install guide and add the section to your product template.

sections/ingredient-list.liquid
{%- liquid
  assign item = product
  if section.settings.product != blank
    assign item = section.settings.product
  endif
  assign parts = section.settings.metafield | split: '.'
  if item != blank
    assign entries = item.metafields[parts[0]][parts[1]].value
  endif
-%}

{%- if entries != blank -%}
  <section id="ing-{{ section.id }}" class="ing">
    <h2 class="ing__heading">{{ section.settings.heading | escape }}</h2>
    <ul class="ing__list" role="list">
      {%- for entry in entries -%}
        <li class="ing__item">
          {%- if entry.photo.value != blank -%}
            {{ entry.photo.value | image_url: width: 240 | image_tag:
              loading: 'lazy',
              widths: '120, 240',
              sizes: '120px',
              alt: ''
            }}
          {%- endif -%}
          <div>
            <h3 class="ing__name">{{ entry.name.value | escape }}</h3>
            {%- if entry.amount.value != blank -%}
              <p class="ing__amount">{{ entry.amount.value | escape }}</p>
            {%- endif -%}
            {%- if entry.note.value != blank -%}
              <p class="ing__note">{{ entry.note.value | escape | newline_to_br }}</p>
            {%- endif -%}
          </div>
        </li>
      {%- endfor -%}
    </ul>
  </section>

  <style>
    #ing-{{ section.id }} { max-width: 960px; margin: 0 auto; padding: {{ section.settings.padding }}px 20px; }
    #ing-{{ section.id }} .ing__heading { margin: 0 0 24px; }
    #ing-{{ section.id }} .ing__list { display: grid; gap: 24px; margin: 0; padding: 0; list-style: none; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
    #ing-{{ section.id }} .ing__item { display: flex; gap: 16px; align-items: flex-start; }
    #ing-{{ section.id }} .ing__item img { flex: none; width: 96px; height: 96px; border-radius: 50%; object-fit: cover; }
    #ing-{{ section.id }} .ing__name { margin: 0 0 4px; }
    #ing-{{ section.id }} .ing__amount { margin: 0 0 6px; font-size: 14px; opacity: .75; }
    #ing-{{ section.id }} .ing__note { margin: 0; }
  </style>
{%- elsif request.design_mode -%}
  <p style="padding: 24px; text-align: center;">{{ section.settings.empty_hint | escape }}</p>
{%- endif -%}

{% schema %}
{
  "name": "Ingredient list",
  "tag": "div",
  "settings": [
    { "type": "text", "id": "heading", "label": "Heading", "default": "What's inside" },
    { "type": "product", "id": "product", "label": "Product", "info": "Leave empty on product templates to use the product being viewed." },
    { "type": "text", "id": "metafield", "label": "Metafield", "default": "custom.ingredients", "info": "A list of metaobjects with fields name, amount, note and photo." },
    { "type": "text", "id": "empty_hint", "label": "Hint shown only in the editor", "default": "This product has no ingredients yet, or the metafield name doesn't match." },
    { "type": "range", "id": "padding", "label": "Vertical padding", "min": 0, "max": 100, "step": 4, "unit": "px", "default": 48 }
  ],
  "presets": [
    { "name": "Ingredient list" }
  ]
}
{% endschema %}

Some things worth copying from this. The section renders nothing for shoppers on a product with no ingredients, so you can leave it on the template for your whole range. The hint prints only in the editor, through request.design_mode. The item fallback lets it run on a landing page with a product chosen, or on a product page with none. And the field names name, amount, note and photo are hard-coded, which is the price of a direct read: if your metaobject calls it title, change the code. That's also the plain limit of metaobjects here, since nothing in the schema can ask the merchant to map fields.

A list of large ingredient names with amounts, a circular photo opening over the list for the selected ingredient
Ingredient Highlights (€4, or €3 with a plan) is the same idea with more motion, and a useful contrast: it takes its ingredients from blocks typed in the editor and doesn't read metafields. That suits one hero product, whereas a metaobject list suits a range sharing the same ingredients.

When you don't need any of this

If you want a single value on your product page and your theme has a Text block, no new section is needed: add the block and connect it. Most metafields can connect through the editor on themes with dynamic sources. A new section earns its keep when a value needs a layout, a repeat or a condition. The Klorr generator can write that file from a description, as long as your description names the exact namespace, key and type. Making sections editable covers what to expose as settings once the section exists, and the Liquid for loop guide covers the looping.

Questions, answered

How do I display a metafield in a Shopify section?

Connect a setting to it with the dynamic source icon in the theme editor, or print it in Liquid: {{ product.metafields.custom.key.value }}. Which to use depends on whether the section needs loops or conditions.

Do I need .value on a Shopify metafield?

Use it when you want the typed content, such as a property of a referenced product or an item in a list. Filters like metafield_tag and metafield_text take the metafield itself, without .value.

Why is my metafield blank in Liquid?

The usual causes: the template has no product object, the namespace or key is wrong, this product has no value, or the value is false, which counts as blank. A metaobject entry that's still in draft also returns nil.

How do I show a rich text metafield?

Use {{ product.metafields.custom.key | metafield_tag }}, which generates the HTML. Connecting it to a richtext setting as a dynamic source also works. Printing .value directly doesn't give you formatted output.

Can I loop through a list metafield in a section?

Yes: {% for item in product.metafields.custom.key.value %}. Use .count on reference lists and the size filter on others to get the length.

Can a section use a metafield on the home page?

Yes, through a product or collection setting, then section.settings.product.metafields.custom.key. The product global is only there on product templates.

Get the whole section written for you

Describe the section you want. Klorr writes the Liquid, the CSS, the schema and every theme-editor setting — code that lives in your theme and stays yours.