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
Visibility Control
Metafields support two visibility levels via thedisplay_on attribute:
Search, sort & filter
Product metafield definitions can opt into SearchProvider participation:
Both default to
false. Set them on the definition (Admin UI, Admin API, or seeds). For an existing catalog with Meilisearch, run rake spree:search:reindex after toggling flags so index settings and product documents pick up new cf_* fields.
Filtering
Any definition with either flag set also becomes filterable on product listings, using itscf_* key as a Ransack predicate. The Store and Admin APIs accept the same keys and the same sort values:
Use
i_cont for case-insensitive matching — plain cont is case-sensitive on PostgreSQL.
Predicates that don’t match the definition’s field type are ignored rather than rejected, as are keys matching no definition. Meilisearch supports the equality, range, and present/blank predicates; substring predicates (i_cont, cont, start, end) apply only to the database provider.
CSV exports honor the same predicates, so exporting a filtered product list matches what the admin sees.
In the admin dashboard, these fields appear in the products table’s column selector, Sort dropdown, and filter panel — hidden by default, so admins opt in per column.
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 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.
Use Metafields when you need type validation, visibility control, admin UI forms, or organized namespacing.
Use Metadata for external system IDs, tracking attribution, syncing with integrations, or simple write-and-forget data that only backend systems need to read.

