Shopify Metafields
Metafields allow you to store additional data on Shopify resources (Products, Orders, Customers, Shops) that aren't included in the default schema (e.g., "washing instructions" for a product).
1. Concepts
- Namespace: Grouping folder (e.g.,
my_app,global). - Key: Specific field name (e.g.,
washing_instructions). - Type: Data type (e.g.,
single_line_text_field,number_integer,json). - Owner: The resource attaching the data (Product ID, Shop ID).
2. Metafield Definitions (Standard)
Always use Metafield Definitions (pinned metafields) when possible. This integrates them into the Admin UI and ensures standard processing.
- Create: Settings > Custom Data > [Resource] > Add definition.
- Access:
namespace.key
3. Accessing in Liquid
To display metafield data on the Storefront:
liquid1<!-- Accessing a product metafield --> 2<p>Washing: {{ product.metafields.my_app.washing_instructions }}</p> 3 4<!-- Accessing a file/image metafield --> 5{% assign file = product.metafields.my_app.size_chart.value %} 6<img src="{{ file | image_url: width: 500 }}" /> 7 8<!-- Checking existence --> 9{% if product.metafields.my_app.instructions != blank %} 10 ... 11{% endif %}
4. Reading via API (GraphQL)
graphql1query { 2 product(id: "gid://shopify/Product/123") { 3 title 4 metafield(namespace: "my_app", key: "instructions") { 5 value 6 type 7 } 8 } 9}
5. Writing via API (GraphQL)
To create or update a metafield, use metafieldsSet.
graphql1mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) { 2 metafieldsSet(metafields: $metafields) { 3 metafields { 4 id 5 namespace 6 key 7 value 8 } 9 userErrors { 10 field 11 message 12 } 13 } 14} 15 16/* Variables */ 17{ 18 "metafields": [ 19 { 20 "ownerId": "gid://shopify/Product/123", 21 "namespace": "my_app", 22 "key": "instructions", 23 "type": "single_line_text_field", 24 "value": "Wash cold, tumble dry." 25 } 26 ] 27}
6. Private Metafields
If you want data to be hidden from other apps and the storefront, use Private Metafields. Note: These cannot be accessed via Liquid directly.
- Use
privateMetafieldqueries/mutations. - Requires explicit
read_private_metafields/write_private_metafieldsscope (rarely used now,app_datametafields are preferred for app-specific valid storage).