home / skills / toilahuongg / shopify-agents-kit / shopify-metafields

shopify-metafields skill

/.claude/skills/shopify-metafields

This skill helps you work with Shopify Metafields by using definitions, accessing via Liquid, and reading or writing with GraphQL.

npx playbooks add skill toilahuongg/shopify-agents-kit --skill shopify-metafields

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.5 KB
---
name: shopify-metafields
description: Guide for working with Shopify Metafields. Covers definitions, storing custom data, accessing via Liquid, and GraphQL mutations.
---

# 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:

```liquid
<!-- Accessing a product metafield -->
<p>Washing: {{ product.metafields.my_app.washing_instructions }}</p>

<!-- Accessing a file/image metafield -->
{% assign file = product.metafields.my_app.size_chart.value %}
<img src="{{ file | image_url: width: 500 }}" />

<!-- Checking existence -->
{% if product.metafields.my_app.instructions != blank %}
   ...
{% endif %}
```

## 4. Reading via API (GraphQL)

```graphql
query {
  product(id: "gid://shopify/Product/123") {
    title
    metafield(namespace: "my_app", key: "instructions") {
      value
      type
    }
  }
}
```

## 5. Writing via API (GraphQL)

To create or update a metafield, use `metafieldsSet`.

```graphql
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
  metafieldsSet(metafields: $metafields) {
    metafields {
      id
      namespace
      key
      value
    }
    userErrors {
      field
      message
    }
  }
}

/* Variables */
{
  "metafields": [
    {
      "ownerId": "gid://shopify/Product/123",
      "namespace": "my_app",
      "key": "instructions",
      "type": "single_line_text_field",
      "value": "Wash cold, tumble dry."
    }
  ]
}
```

## 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 `privateMetafield` queries/mutations.
-   Requires explicit `read_private_metafields` / `write_private_metafields` scope (rarely used now, `app_data` metafields are preferred for app-specific valid storage).

Overview

This skill guides you through using Shopify Metafields to store and surface custom data on Products, Orders, Customers, and the Shop. It explains core concepts, how to define pinned Metafields, read them in Liquid and GraphQL, and create/update them with GraphQL mutations. It also covers private metafields and access considerations.

How this skill works

Metafields attach key/value pairs to Shopify resources and are organized by namespace and key with an associated type and owner. Use Metafield Definitions to register fields in the Admin so they appear in the UI and follow predictable types. Read values on the storefront with Liquid, query them via GraphQL, and write or update them using the metafieldsSet mutation (or privateMetafield endpoints for hidden data).

When to use it

  • Store custom product attributes not present in Shopify’s default schema (e.g., washing instructions).
  • Attach app-specific data to resources without altering primary records.
  • Expose structured data to the storefront or hide data using private metafields.
  • Provide alternate content like size charts, downloadable files, or JSON blobs for app logic.
  • Migrate or sync third-party metadata to Shopify for storefront rendering or backend workflows.

Best practices

  • Prefer Metafield Definitions (pinned metafields) so fields appear in Admin and enforce types.
  • Use clear namespace and key naming (e.g., my_app.washing_instructions) to avoid collisions.
  • Choose the most specific type available (file, single_line_text_field, number_integer, json) to get correct validation and rendering.
  • Check for existence in Liquid before rendering to avoid blank output.
  • Use private metafields or app_data for data that must remain hidden from other apps and the storefront.

Example use cases

  • Show a product’s washing instructions on the product page via product.metafields.my_app.washing_instructions.
  • Attach a size chart file to a product and render it as an image using the file’s image_url filter.
  • Store complex structured settings in a json metafield for an app to read and apply at runtime.
  • Update pricing notes or internal flags using metafieldsSet in a back-office integration.
  • Save per-customer preferences or internal flags as private metafields inaccessible to storefront Liquid.

FAQ

When should I use private metafields vs pinned Metafield Definitions?

Use pinned Metafield Definitions when you want the field surfaced in Admin and usable in Liquid. Use private metafields when the data must be hidden from other apps and the storefront; private access requires specific scopes.

How do I update a metafield via the API?

Use the GraphQL metafieldsSet mutation with ownerId, namespace, key, type, and value. Check userErrors in the response for validation issues.