Conditional metafield definitions
Constraints allow metafield definitions to be applied to a subset of resources.
How it works
Anchor link to section titled "How it works"By default, metafield definitions apply to every resource on their owner type. For example, Product
metafield definitions apply to all products and appear on all product detail pages in the merchant admin.
However, some metafield definitions should only apply to a subset of resources. For example, Shoe size
is a valid metafield for Shoes
products but wouldn't apply to Sweaters
.
Metafield definition constraints provide a way to conditionally apply definitions based on the characteristics of a resource. For example, each category metafield comes with a set of constraints, which determine what product categories the metafield applies to.
At the core of the conditional metafields system are constraint subtypes. Constraint subtypes are key | value
pairs that identify a "subtype" of a metafield owner type.
For example, because aa-8
is the ID of the Shoes
product category, the Shoes
constraint subtype is identified by:
Currently, Shopify only supports constraint subtypes that correspond to product categories on Product
metafield definitions. These constraint subtypes all have a key
equal to category
.
The constraints
field can be used to view a metafield definition's constraints.
The constraintSubtype
and constraintStatus
arguments can be used to filter metafield definitions queries based on constraints.
The constraintSubtype
argument returns only metafield definitions that apply to the identified subtype. Metafield definitions are applicable to a constraint subtype if one of the following criteria is met:
- The metafield definition has a constraint matching the
constraintSubtype
. - The metafield definition does not have any constraints, which means the definition applies to all constraint subtypes.
The constraintStatus
argument filters metafields based on whether they are constrained or unconstrained. constraintStatus
accepts the following values:
CONSTRAINED_ONLY
UNCONSTRAINED_ONLY
CONSTRAINED_AND_UNCONSTRAINED
constraintSubtype
and constraintStatus
can also be used on the standardMetafieldDefinitionTemplates
query in order to query metafield standard templates based on their constraints.