Create an automated collection with metafields
An automated collection, also known as a smart collection, is a grouping of products that's defined by a set of rules. Shopify automatically changes the contents of an automated collection based on the configured rules. You can create rules with metafield definitions to automatically update the contents of an automated collection based on product metafields and variant metafields.
In this guide, you'll learn how to configure an automated collection with metafield definition rules by creating an automated collection that contains all products that are made out of silk and on sale.
Requirements
Anchor link to section titled "Requirements"- Your app can make authenticated requests to the GraphQL Admin API.
- Your app is using API version 2023-01 or higher.
- You've created products in your store.
Step 1: Create the metafield definitions with the automated collection setting enabled
Anchor link to section titled "Step 1: Create the metafield definitions with the automated collection setting enabled"For this example, you'll need to create two metafield definitions for the product materials and sale status. You can specify that the metafield definitions can be used for automated collection conditions by setting the useAsCollectionCondition
setting to true
.
The following code uses the metafieldDefinitionCreate
mutation to create a materials metafield definition of type list.single_line_text_field
. This metafield will contain the list of materials used to make the product.
The following code uses the metafieldDefinitionCreate
mutation to create an On sale
metafield definition of type boolean
. This metafield will indicate whether the product is on sale.
Step 2: Populate the metafields on your products
Anchor link to section titled "Step 2: Populate the metafields on your products"To populate the metafields, use the productUpdate
mutation and include the metafields that you created in the previous step in the mutation input.
Learn more about how to populate metafields.
Step 3: Verify the metafield definitions are available as collection conditions
Anchor link to section titled "Step 3: Verify the metafield definitions are available as collection conditions"You can use the collectionRulesConditions
query to verify the metafield definitions are set up as collection conditions.
Step 4: Create an automated collection with the metafield definition conditions
Anchor link to section titled "Step 4: Create an automated collection with the metafield definition conditions"After confirming that the metafield definitions are configured as collection conditions, you can create an automated collection that uses these metafield definitions as conditions.
- Learn about standard metafield definitions that store common product data and can also be used to configure automated collections.