Metafields in themes

Read a store's custom fields in sections, and declare the fields your theme needs.

Metafields are custom fields a store adds to its products, variants, collections, categories, pages, blog posts, blog categories, vendors, brands and the store itself. Fields marked visible on the storefront are available to sections.

Read metafields#

Read them under .metafields.<namespace>.<key> on any item that has them, and on shop for the store's own fields:

vascula
{% assign specs = product.metafields.specs %}
{% if specs.material %}
  <dl class="specs">
    <dt>{{ "specs.material" | t }}</dt>
    <dd>{{ specs.material }}</dd>
  </dl>
{% endif %}

<p>{{ shop.metafields.brand.tagline }}</p>

Values arrive ready to use: numbers as numbers, lists as lists, choices as their labels in the shopper's language when translation is enabled, files as media objects, and product/variant references as visible cards. Reference resolution is batched across detail and listing contexts, capped at 256 distinct product/variant references per request and one level deep. Unavailable or over-budget references are absent; lists omit them. Check that a value is present before printing it. See CTX: Metafields for types, translation fallback and resolution limits.

vascula
{% assign pair = product.metafields.merch.pairs_with %}
{% if pair %}
  <a href="{{ pair.url }}">{{ pair.title }} · {{ pair.price.current | money }}</a>
{% endif %}

A referenced product that is not available on the storefront reads as nothing.

Declare the fields your theme needs#

Declare the metafields your theme reads in config/metafields.json, as rule metafields.declared of the theme contract requires. The file looks like this:

json
[
  { "owner_type": "product", "namespace": "specs", "key": "material",
    "type": "string", "name": "Material", "translatable": true },
  { "owner_type": "product", "namespace": "specs", "key": "fit",
    "type": "single_select", "name": "Fit",
    "choices": [ { "key": "slim", "label": "Slim" }, { "key": "regular", "label": "Regular" } ] }
]

Field

Meaning

owner_type

product, variant, collection, category, page, blog_post, blog_category, vendor, brand or store.

namespace, key

The field's address: product.metafields.<namespace>.<key>. Up to 64 characters each.

type

The field type, such as string, integer, boolean, single_select or product_reference. See CTX: Metafields.

name

The name merchants see, up to 100 characters.

description

Optional help text.

choices

For single_select and list.single_select: { "key", "label" } pairs.

translatable

For text and choice types: merchants can translate the values.

metaobject_type

For metaobject_reference and list.metaobject_reference: the handle of the custom content type the field points to, declared in config/metaobjects.json or already in the store. See Custom content.

A theme can declare up to 40 fields. If the store already has a field with the same address but a different type, installation stops and names the field. A Theme Store update creates the fields its new version adds; the update preview lists them under metafield_definitions_added, and a type clash stops the update the same way. Existing definitions and values are never changed, and uninstalling a theme or undoing an update never deletes metafield values.

Platform metafield block#

Merchants show a metafield without theme code by adding the platform's Metafield block (theme:metafield) in the theme editor or with a connected AI tool (Store MCP add_block). Every theme has it: any section that accepts app blocks (accept_app_blocks) or theme blocks (accept_theme_blocks) and renders {% blocks %} takes it, whatever its theme_blocks list says. The storefront writes its markup for every value type; your theme only styles it.

Rendered markup html
<div class="tf-metafield tf-metafield--string" data-tf-metafield="specs.material">
  <span class="tf-metafield__label">Material</span>
  <div class="tf-metafield__value"><span class="tf-metafield__text">Organic linen</span></div>
</div>

Value type

Markup inside tf-metafield__value

text, choices

<span class="tf-metafield__text"> (line breaks kept; a choice shows its label in the shopper's language)

integer, decimal

<span class="tf-metafield__number"> in the shopper's number format

date, datetime

<time class="tf-metafield__date" datetime> in the shopper's long date format

boolean

<span class="tf-metafield__boolean" data-tf-value="true|false"> with ✓ or ✗

url

<a class="tf-metafield__link">

file

<img class="tf-metafield__image">, <video class="tf-metafield__video"> or <a class="tf-metafield__file" download>

product, variant, page, blog post, collection and custom content references

<a class="tf-metafield__reference"> to the item, named by its title (a variant opens selected; a custom content entry whose type has no web pages is a <span> with its display name)

rich_text

<div class="tf-metafield__rich-text"> with the stored, sanitized markup

color

<span class="tf-metafield__color"> with a round tf-metafield__swatch and the #rrggbb value

json

<pre class="tf-metafield__json">

list.*

<ul class="tf-metafield__list"> with one <li> per item

The block's wrapper also carries tf-metafield--<type> (dots and underscores as hyphens, for example tf-metafield--list-file-reference), so you can style each type. Its settings: Show a field of (owner: product, collection, category, vendor, brand, page, blog_post or store), the field (field_<owner>, a storefront-visible definition of that owner as namespace.key), Label and Show label. The owner is the page's own item of that type; on a product page vendor, brand and category are the product's (its first category). store works on every page.

The label is the definition's name, translated per storefront language (definition name translations in Store Admin, translation CSV or the Store API), unless the merchant writes a label in the block; that label is theme text, translated with the theme's text translations. An empty value renders nothing for shoppers. In the theme editor the block says why it is empty: no field chosen, no such owner on the page, a field that is not storefront visible, or no value.

Theme contract rule metafields.block: the section that shows the page's item (on a product, collection, category, vendor, brand, page or blog post template, the first section reading that item) must accept blocks so merchants can place the Metafield block there. The metafield-block check warns when it does not.

If your theme has its own block that shows a picked metafield, retire it for the platform block with a rename_block migration to theme:metafield: a renamed instance's field setting becomes the product field (field_product), and its label is kept, a theme string (@t:) included. List the old block's defaults in source_defaults so instances that never saved them keep what they showed.

sections/main-product schema json
"migrations": [
  { "id": "custom-field-to-platform-metafield", "op": "rename_block",
    "from_type": "custom_field", "to_type": "theme:metafield",
    "source_defaults": { "field": "details.material", "label": "@t:sections.main_product.custom_field.label" } }
]

Use metafields elsewhere#

  • Connect a text setting to a metafield in the editor: see dynamic sources.
  • Filterable metafields appear as filters on product listings.