Skip to main content

Conditional metafield definitions

Constraints allow metafield definitions to be applied to a subset of resources.


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 Shopify 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 standard 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 gid://shopify/TaxonomyCategory/aa-8 is the ID of the Shoes product category, the Shoes constraint subtype is identified by:

{
key: "category",
value: "gid://shopify/TaxonomyCategory/aa-8,
}

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.


Anchor to Find product category IDs for constraints inputsFind product category IDs for constraints inputs

You might want to create a custom Shoelace material conditional metafield that applies to Shoes and the children categories of Shoes, such as Boots and Sneakers.

You need to determine the IDs of the Shoes category and its children. This data can be extracted directly from our product-taxonomy repository.

Alternatively, you can determine taxonomy category IDs using the GraphQL Admin API. For example, if you know that the ID for Shoes is gid://shopify/TaxonomyCategory/aa-8, then you can find the IDs for the children of Shoes using the following query:

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL query

query ShoesChildrenIds {
taxonomy {
categories(first: 250, childrenOf: "gid://shopify/TaxonomyCategory/aa-8") {
nodes {
name
id
}
}
}
}

JSON response

{
"taxonomy": {
"categories": {
"nodes": [
{
"name": "Athletic Shoes",
"id": "gid://shopify/TaxonomyCategory/aa-8-1"
},
{
"name": "Heels",
"id": "gid://shopify/TaxonomyCategory/aa-8-10"
},
{
"name": "Baby & Toddler Shoes",
"id": "gid://shopify/TaxonomyCategory/aa-8-2"
},
{
"name": "Boots",
"id": "gid://shopify/TaxonomyCategory/aa-8-3"
},
{
"name": "Sandals",
"id": "gid://shopify/TaxonomyCategory/aa-8-6"
},
{
"name": "Slippers",
"id": "gid://shopify/TaxonomyCategory/aa-8-7"
},
{
"name": "Sneakers",
"id": "gid://shopify/TaxonomyCategory/aa-8-8"
},
{
"name": "Flats",
"id": "gid://shopify/TaxonomyCategory/aa-8-9"
}
]
}
}
}
}

Anchor to Create a custom conditional metafield definitionCreate a custom conditional metafield definition

You can then use the constraints field on metafieldDefinitionCreate to create the conditional metafield.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
metafieldDefinitionCreate(definition: $definition) {
createdDefinition {
name
ownerType
namespace
key
constraints {
key
values(first: 10) {
nodes {
value
}
}
}
}
}
}

Variables

{
"definition": {
"name": "Shoelace material",
"namespace": "custom",
"key": "shoelace_material",
"type": "single_line_text_field",
"ownerType": "PRODUCT",
"constraints": {
"key": "category",
"values": [
"gid://shopify/TaxonomyCategory/aa-8",
"gid://shopify/TaxonomyCategory/aa-8-1",
"gid://shopify/TaxonomyCategory/aa-8-2",
"gid://shopify/TaxonomyCategory/aa-8-3",
"gid://shopify/TaxonomyCategory/aa-8-6",
"gid://shopify/TaxonomyCategory/aa-8-7",
"gid://shopify/TaxonomyCategory/aa-8-8",
"gid://shopify/TaxonomyCategory/aa-8-9",
"gid://shopify/TaxonomyCategory/aa-8-10"
]
}
}
}

JSON response

{
"createdDefinition": {
"name": "Shoelace material",
"ownerType": "PRODUCT",
"namespace": "custom",
"key": "shoelace_material",
"constraints": {
"key": "category",
"values": {
"nodes": [
{
"value": "aa-8"
},
{
"value": "aa-8-1"
},
{
"value": "aa-8-2"
},
{
"value": "aa-8-3"
},
{
"value": "aa-8-6"
},
{
"value": "aa-8-7"
},
{
"value": "aa-8-8"
},
{
"value": "aa-8-9"
},
{
"value": "aa-8-10"
}
]
}
}
}
}

Anchor to Add & remove constraints on a metafield definitionAdd & remove constraints on a metafield definition

If you would like to edit the constraints of a metafield definition, you can use the constraintsUpdates field on metafieldDefinitionUpdate.

The constraintsUpdates field handles both the creation and deletion of constraints. For example, you might realize that Shoelace material doesn't make sense on your Flats products because Flats don't have shoelaces. You might have also started selling a line of Shoelaces products and would like to track the Shoelace material of these new products.

The following example shows how you could update the Shoelace material metafield so that it is no longer constrained to Flats but is added to Shoelaces.

Note

If constraints already exist on the definition, then the key field is optional. If you're adding constraints to a definition that was previously unconstrained, then key must be included.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation UpdateMetafieldDefinition($definition: MetafieldDefinitionUpdateInput!) {
metafieldDefinitionUpdate(definition: $definition) {
updatedDefinition {
name
ownerType
namespace
key
constraints {
key
values(first: 10) {
nodes {
value
}
}
}
}
}
}

Variables

{
"definition": {
"namespace": "custom",
"key": "shoelace_material",
"ownerType": "PRODUCT",
"constraintsUpdates": {
"values": [
{ "delete": "gid://shopify/TaxonomyCategory/aa-8-9" },
{ "create": "gid://shopify/TaxonomyCategory/aa-7-6" }
]
}
}
}

JSON response

{
"updatedDefinition": {
"name": "Shoelace material",
"ownerType": "PRODUCT",
"namespace": "custom",
"key": "shoelace_material",
"constraints": {
"key": "category",
"values": {
"nodes": [
{
"value": "aa-8"
},
{
"value": "aa-8-1"
},
{
"value": "aa-8-2"
},
{
"value": "aa-8-3"
},
{
"value": "aa-8-6"
},
{
"value": "aa-8-7"
},
{
"value": "aa-8-8"
},
{
"value": "aa-8-10"
},
{
"value": "aa-7-6"
}
]
}
}
}
}

Anchor to Unconstrain a metafield definitionUnconstrain a metafield definition

If you no longer want a definition to be constrained, then you can also use the constraintsUpdates field to remove all of the definition's constraints. The constraints can be identified individually or you can set both the key and values fields to null to delete all constraints.

If a definition is unconstrained, then the definition applies to all resources and appears on all resource pages in the Shopify admin.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL mutation

mutation UpdateMetafieldDefinition($definition: MetafieldDefinitionUpdateInput!) {
metafieldDefinitionUpdate(definition: $definition) {
updatedDefinition {
name
ownerType
namespace
key
constraints {
key
values(first: 10) {
nodes {
value
}
}
}
}
}
}

Variables

{
"definition": {
"namespace": "custom",
"key": "shoelace_material",
"ownerType": "PRODUCT",
"constraintsUpdates": {
"key": null,
"values": null
}
}
}

JSON response

{
"updatedDefinition": {
"name": "Shoelace material",
"ownerType": "PRODUCT",
"namespace": "custom",
"key": "shoelace_material",
"constraints": {
"key": null,
"values": {
"nodes": []
}
}
}
}

Anchor to Query for metafield definitions based on constraintsQuery for metafield definitions based on 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.

POST https://{shop}.myshopify.com/api/{api_version}/graphql.json

GraphQL query

query NecklaceMetafieldDefinitions {
metafieldDefinitions(
first: 5,
ownerType: PRODUCT,
constraintSubtype: {
key: "category",
value: "gid://shopify/TaxonomyCategory/aa-6-8"
},
constraintStatus: CONSTRAINED_ONLY
) {
edges {
node {
name
namespace
key
}
}
}
}

JSON response

{
"metafieldDefinitions": {
"edges": [
{
"node": {
"name": "Color",
"namespace": "shopify",
"key": "color-pattern"
}
},
{
"node": {
"name": "Target gender",
"namespace": "shopify",
"key": "target-gender"
}
},
{
"node": {
"name": "Age group",
"namespace": "shopify",
"key": "age-group"
}
},
{
"node": {
"name": "Jewelry material",
"namespace": "shopify",
"key": "jewelry-material"
}
},
{
"node": {
"name": "Jewelry type",
"namespace": "shopify",
"key": "jewelry-type"
}
}
]
}
}

Was this page helpful?