How to Filter Products Using Shopify’s GraphQL API and Metafields
Introduction
Using the Shopify GraphQL API, you can enhance your store or app’s functionality by filtering products based on various attributes, including custom information stored in Shopify product metafields. This enables more refined product searches and facilitates access to relevant data. Properly applying filters enhances both efficiency and outcomes.
1. Filtering Products Based on Standard Attributes
Shopify allows you to filter products using various standard attributes, such as price, title, tags, and availability. When utilizing the Shopify GraphQL API, you can apply different filters to search for products by custom data (metafields) and other product details.
Basic Product Filters
The following standard fields can be used to filter products:
- Title: Filter products by their title.
- Vendor: Filter products by vendor.
- Product Type: Filter products by product type.
- Status: Filter products by status (e.g., Active, Archived).
- Tag: Filter products by tags.
- Price Range: Filter products by price range.
- SKU: Filter products by SKU.
- Inventory Quantity: Filter products by available inventory levels.
Example Query:
2. Metafield Filters
Filter by Namespace and Key
You can find products that have a metafield with a specific namespace and key.
{
products(first: 10, query: “metafields.OSCP:gridRules”) {
edges {
node {
id
title
metafields(first:250, namespace: “gridRules”) {
key
value
}
}
}
}
}
Filter by Metafield Value
You can filter products based on metafield values.
Example Query:
In the given example, the filter is not applied to metafields or metafield values, it is applied to the product title.
Ready to launch your Shopify store? We will create a standout site with our development and app development to build a store that sells.
Contact us now and get your store up and running fast!
Combine Multiple Filters
You can use operators like AND and OR to combine different filters. For instance, retrieve products with a title starting with “OSC” and a status of “ACTIVE.”
This combined filter applies only to basic product filters, not metafield filters.
You can also combine metafield filters with other product filters (e.g., product status, vendor). However, they will apply to product details, not metafields.
Example Query:
Complex Query Example
Here is an example of a more complex query combining multiple filters:
Conclusion
Shopify’s GraphQL API and metafields provide a powerful toolset for applying highly customizable filters when querying products. Whether filtering by standard attributes or leveraging the flexibility of Shopify product metafields for custom data, you can create advanced searches tailored to your business needs.