Skip to main content

Overview

Metafields provide a flexible, type-safe system for adding custom structured data to Spree resources. Unlike metadata which is simple JSON storage, metafields are schema-defined with strong typing, validation, and visibility controls. Use metafields for:
  • Product specifications (manufacturer, material, dimensions)
  • Custom business logic fields
  • Integration data from external systems
  • Order-specific custom attributes
Metafields are available from Spree 5.2 onwards.

Architecture

  • MetafieldDefinition — the blueprint that defines the data type, target resource, and visibility
  • Metafield — stores the actual value for a specific resource instance

Data Types

TypeDescriptionExample Values
Short TextBrief text fieldsSKU codes, brand names, tags
Long TextLonger text contentCare instructions, notes
Rich TextFormatted HTML contentProduct descriptions with formatting
NumberNumeric valuesWeight, quantity, ratings
BooleanTrue/false flagsIs featured, requires signature
JSONStructured dataConfiguration, complex objects

Visibility Control

Metafields support three visibility levels via the display_on attribute:
VisibilityStore APIAdmin APIUse Case
bothYesYesPublic product specifications
front_endYesNoCustomer-facing data
back_endNoYesInternal notes, integration IDs

Supported Resources

Metafields can be attached to most Spree resources including Products, Variants, Orders, Line Items, Taxons, Payments, Shipments, Gift Cards, Store Credits, and more.
Custom resources can also support metafields. See the Customization Quickstart for details.

Namespaces

Namespaces organize metafields into logical groups and prevent key conflicts:
NamespaceExample KeysPurpose
propertiesmanufacturer, material, fitProduct specifications
shopifyproduct_id, variant_idIntegration data
flagsfeatured, requires_approvalFeature flags
customgift_message, delivery_notesBusiness-specific fields
Namespace and key are automatically normalized to snake_case.

Store API

Metafields with display_on set to both or front_end are included in Store API responses when you request the metafields expand:
const product = await client.products.get('spree-tote', {
  expand: ['metafields'],
})

product.metafields?.forEach(metafield => {
  console.log(metafield.key)   // "properties.manufacturer"
  console.log(metafield.name)  // "Manufacturer"
  console.log(metafield.value) // "Wilson"
  console.log(metafield.type)  // "short_text"
})
Response:
{
  "id": "prod_86Rf07xd4z",
  "name": "Spree T-Shirt",
  "metafields": [
    {
      "id": "mf_k5nR8xLq",
      "name": "Manufacturer",
      "key": "properties.manufacturer",
      "type": "short_text",
      "value": "Wilson"
    },
    {
      "id": "mf_m3Rp9wXz",
      "name": "Material",
      "key": "properties.material",
      "type": "short_text",
      "value": "100% Cotton"
    }
  ]
}
The display_on attribute is intentionally excluded from Store API responses for security.

Admin Panel Management

Managing Definitions

Navigate to Settings → Metafield Definitions in the Admin Panel to create and manage metafield definitions. Select the resource type, enter namespace and key, choose the data type, and set visibility.

Managing Values

When editing a resource (e.g., a product), metafields appear in a dedicated section. The admin panel automatically builds forms for all defined metafields.

Metafields vs Metadata

FeatureMetafieldsMetadata
StructureStrongly typed, schema-definedFlat key-value JSON
ValidationType-specific validationNone
VisibilityConfigurable (front/back/both)Write-only in Store API
Admin UIDedicated management formsJSON preview
Data Types6 specific typesAny JSON-serializable data
OrganizationNamespaced (namespace.key)Flat structure
Use Metafields when you need type validation, visibility control, admin UI forms, or organized namespacing. Use Metadata for external system IDs, tracking attribution, or simple write-and-forget data.
Product Properties are deprecated and will be removed in Spree 6.0. For new projects, always use Metafields. For existing projects, plan to migrate using the migration guide.
  • Metadata — Simple key-value metadata
  • Products — Product catalog
  • Events — Subscribe to metafield events