Skip to main content

Overview

Sooner or later you need to store something Spree doesn’t have a field for. A fabric composition. A care instruction. A gift message. Custom fields are how you do that without changing the database. You declare a field once — its name, its type, who can see it — and from then on it can be set on any record of that kind, edited in the dashboard, returned by the API, and searched or filtered like any built-in field.

Definitions and values

There are two halves, and it helps to keep them straight:
  • A definition is the declaration: “products have a Material, it is short text, shoppers may see it.” You create it once.
  • A custom field is one record’s answer: this product’s material is 100% Cotton.
Because the definition carries the type and the label, the dashboard can build an editing form for it automatically, and your storefront gets a value it can trust the shape of.

Field types

A date_time written without an offset is read in the store’s time zone. Dates are parsed as ISO 8601 only, never by locale. A choice field stores the choice’s value, not its label, so relabelling a choice changes no data. For product attributes that shoppers browse or filter by, such as material or color, use option types instead: they carry translations, ordering and storefront filters. A definition’s field_type and resource_type are fixed once it is created. An update that sends a different one is refused with 422; create a new definition and move the values instead.

Who can see it

storefront_visible decides whether a field ever leaves the back office: New definitions are admin-only: storefront_visible defaults to false, so send true for a field shoppers should see. Order, customer and line item fields can also be storefront_writable: shoppers fill them in through the Store API, an order field at checkout, a customer field at registration and from their account, and a line item field when they add a product to the cart. A writable field must also be storefront_visible. See Fields shoppers fill in.
storefront_visible: false genuinely withholds the field from the Store API — it is not hidden in the response, it is absent. Treat it as the boundary between what a customer may read and what only staff may.

Declaring a field

namespace keeps groups of fields apart, so an integration’s product_id never collides with yours. Together they form the key you’ll see in responses: properties.material. Both are normalized to snake_case. Definitions can also be managed in the dashboard under Settings → Custom Fields, which is where merchants usually add them.

Validation rules

validations sets rules a value must meet when it is written:
A value that breaks a rule is refused with 422, the error naming the field. Rules are checked on new writes only: a value saved before a rule changed stays readable, and is checked again when it is next written. Sending validations replaces every rule; send null to remove them all.

Setting and reading values

Reading them from a storefront is an expand on whatever you already fetch:
Response
Each value arrives with its own label and type, so you can render a spec table straight from the array without hardcoding which fields exist. The same expand works on every Store API record that can carry custom fields:
  • Catalog: products, variants, option types and values, media, categories, collections and sellers
  • Checkout: carts, line items, delivery methods, payment methods, fulfillments and payments
  • A customer’s own records: their account, addresses, orders, saved cards and payment sources, gift cards, store credits, returns, claims, companies and newsletter subscription
On a marketplace, for example, merchants can manage seller merchandising from the dashboard — a merchandising.navigation_position number that picks featured sellers and orders them:

Fields shoppers fill in

A delivery date, a gift message or a customer’s date of birth is input the shopper gives you. Mark the definition storefront_writable, and the Store API accepts it. Any field type except json and rich_text can be writable.
  • Order fields are written on the cart during checkout. Completing checkout copies them to the order, and on a marketplace to every seller’s order, where sellers read them.
  • Customer fields are written at registration and from the customer’s account.
A storefront renders its form from the list of writable fields, which carries each field’s type, label and rules:
Values are written by each field’s full_key, in custom_fields on the cart, the registration or the account update:
  • A key that is not a writable field of this store is refused with 422. The API never creates a definition from a shopper’s key.
  • Each value is checked against the field’s rules, and is at most 5,000 characters whatever the rules say. Numbers are sent as decimal strings, dates in ISO 8601.
  • A blank value removes the field. Fields the request does not name keep their values, and a field may be named only once per request.
  • Read them back with expand: ['custom_fields'] on the cart, the order or the customer. Each value comes with a display_value: plain text with a choice field’s labels and rich text without its markup.
required is enforced where the shopper writes:
  • A required order field the cart has no value for is a checkout requirement, with the code custom_field_required and the field’s full_key as field. Checkout cannot complete until it is filled in. Orders that staff key in are not held up by it.
  • A registration that leaves out a required customer field is refused with 422, and no account is created. An account update cannot blank one.
Staff and Admin API writes treat required as advisory, as everywhere else. A refused write returns 422 and saves nothing. One response names every field that was refused, each under custom_fields. followed by the field’s full_key, so a form can show the text beside the right input. The message reads with each field’s label:
A key that names no field a shopper can fill in has no field to point at, so it is reported by its place in the request, as custom_fields[0].key. The texts come in the request’s locale, like every other Store API validation error.

Fields shoppers fill in per item

An engraving, a monogram or a gift note for one item is a line item field. The product type decides which products take it: add a writable line item definition to a type’s custom fields, mark it required there if every product of the type needs it, and the type’s products ask for it.
The product type’s list replaces the whole set, so send the product’s own fields in it too. A storefront reads which fields a product takes from the product itself, with expand: ['line_item_fields']. Each comes with its type, label and rules, in the type’s order, and validations.required says whether this product requires it:
Values go in custom_fields wherever the Store API takes a cart item: adding an item, updating one, and each entry of items on cart create and update.
  • The rules above apply: unknown keys are refused, values are checked and capped, a blank value removes a field.
  • A field the product’s type does not list is refused with 422, under custom_fields. and its full_key. Removing a field from a type stops new values; values already on lines stay.
  • Items whose values differ are separate lines. An add joins a line of the same variant only when that line holds the same values, so a cart can hold several lines of one variant. Updating a line by its id changes its values in place. An items entry with quantity 0 and only a variant_id removes every line of the variant.
  • A refused value in items refuses the whole request, reported under the item’s place: items[0].custom_fields.custom.engraving.
  • Read the values back with expand: ['items.custom_fields'] on the cart or the order.
  • A required field a line has no value for is a custom_field_required checkout requirement on the cart step. It names the line in line_item_id, beside the field’s full_key. Adding the product to the cart is never refused for it.
  • A field never changes the price. Charge for personalization with a dearer variant or an add-on product.
Completing checkout copies each line’s values to the order, where staff see them under the line and can correct them, and sellers see them read-only on their order.

In emails and webhooks

An order’s storefront-visible custom fields, and those of each of its items, reach everyone who handles the order outside the dashboard:
  • Emails. The default order confirmation, cancellation and new order notification print each field as Label: value, with dates in the store’s time zone, as do the confirmation and notification of an order group (a split marketplace order). Each item’s fields print under the item. Custom templates read them from order.custom_fields and item.custom_fields; see Email Variables.
  • Webhooks. Order events (order.*) and customer events (user.created, user.updated, customer.password_reset) carry the record’s storefront-visible fields in data.custom_fields, in the same shape as the Store API’s expand=custom_fields. Each item in data.items carries its own in custom_fields. order_group.completed carries them on each of its orders and their items. user.deleted and customer.anonymized carry an empty list, since the customer’s values are erased with the account.
Admin-only fields (storefront_visible: false) appear in neither, not even in the emails sent to staff. Emails get forwarded and pasted into support tickets, so anything internal stays in the dashboard. Changing only a custom field’s value does not send a webhook of its own. The new value arrives with the next event of the order or customer.

Searching, sorting and filtering

A custom field can take part in product listings — but only if you ask, because indexing everything by default would be wasteful. Setting either flag also makes the field filterable. Filter and sort with the field’s cf_ key, on both APIs:
A choice field that allows several values matches when any of its values does. Use i_cont for case-insensitive matching. A comparison that doesn’t suit the field’s type is ignored rather than rejected, so a stale filter in a saved view can’t break a page.
If you use Meilisearch, re-index after changing these flags so the new fields are picked up. Substring matching (i_cont, cont, start, end) is a database-provider feature; Meilisearch handles equality, ranges and presence. See Search & Filtering.
In the dashboard, fields with these flags become available in the product table’s column picker, sort menu and filter panel — off by default, so merchants opt in per column.

What can carry custom fields

Most things you’d want to annotate: products, variants, orders, line items, customers, categories, payments, fulfillments, gift cards and store credits, among others. Settings → Custom Fields lists every type your installation supports; types no dashboard screen edits appear under Through the API only, for integrations to fill in. Merchants fill values in from the record’s own screen in the dashboard: Sheets show the card only once the store defines a field for that type, so a store that uses none sees no empty sections. Staff who can view a record but not edit it don’t see its custom fields: reading them needs edit access. Each record type in the table above has the same nested endpoint. The Admin SDK reaches any of them through one accessor, named by the record’s type:

Definitions belong to a store

A definition is owned by the store it was created in. Each store keeps its own set, so two stores can both define custom.material without colliding, and neither can read or edit the other’s. Everything that reads the schema — the dashboard, CSV exports and imports, and product sorting and filtering — reads the current store’s definitions.

Custom fields or metadata?

Spree has two ways to store your own data, and they are not competing — they solve different problems. Put it simply: custom fields are for people, metadata is for machines. If a merchant should type it, define a custom field. If it is a sync token that only your integration reads, use metadata. If it is the key another system knows the record by, use an external reference.