Overview
Metafields provide a flexible, type-safe system for adding custom structured attributes to Spree models. 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
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
| Type | Description | Example Values |
|---|---|---|
| Short Text | Brief text fields | SKU codes, brand names, tags |
| Long Text | Longer text content | Care instructions, notes |
| Rich Text | Formatted HTML content | Product descriptions with formatting |
| Number | Numeric values | Weight, quantity, ratings |
| Boolean | True/false flags | Is featured, requires signature |
| JSON | Structured data | Configuration, complex objects |
Visibility Control
Metafields support two visibility levels via thedisplay_on attribute:
| Visibility | Store API | Admin API | Use Case |
|---|---|---|---|
both | Yes | Yes | Public product specifications |
back_end | No | Yes | Internal 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:| Namespace | Example Keys | Purpose |
|---|---|---|
properties | manufacturer, material, fit | Product specifications |
shopify | product_id, variant_id | Integration data |
flags | featured, requires_approval | Feature flags |
custom | gift_message, delivery_notes | Business-specific fields |
Namespace and key are automatically normalized to snake_case.
Store API
Metafields withdisplay_on set to both are included in Store API responses when you request the custom_fields expand:
Metafields in API are called Custom Fields as we plan to rename Metafields to Custom Fields completely in Spree 6.0.
The
display_on attribute is intentionally excluded from Store API responses for security.Admin 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. Definitions are also managed via the Admin API.storefront_visible: true is equivalent to display_on: both — it exposes the field to the Store API:
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. To set a value programmatically, use the resource’s nestedcustomFields accessor (parent ID first):
Metafields vs Metadata
Spree has two permanent, complementary systems for custom data — metadata for machines, metafields for humans. They serve different purposes and are not interchangeable. Neither is going away.| Feature | Metafields | Metadata |
|---|---|---|
| Purpose | Merchant-defined structured attributes | Developer escape hatch — integration IDs, sync state |
| Schema | Defined via MetafieldDefinitions | Schemaless JSON — no definition required |
| Validation | Type-specific (text, number, boolean, etc.) | None — accepts any JSON-serializable data |
| Visibility | Configurable (admin-only or public) | Write-only in Store API, readable in Admin API |
| Admin UI | Dedicated management forms | JSON preview |
| Data Types | 6 specific types | Any JSON value |
| Organization | Namespaced (namespace.key) | Flat key-value structure |
| Queryable | Via SQL joins, Ransack scopes, search providers | Via JSONB operators (PostgreSQL) |

