Metafields on any owner that has them (products, variants, orders, customers, draft orders, the shop, ...): set (upsert), get one, list and delete. Needs the owner resource's read/write scope.

Values

MAX_METAFIELDS_PER_SET

MAX_METAFIELDS_PER_SET: Int 25

METAFIELD_FIELDS

METAFIELD_FIELDS: Str "id namespace key type value compareDigest updatedAt"

Functions

build-metafield-input

fn (metafield: Map): Map

One MetafieldsSetInput: {ownerId, namespace, key, type, value} plus optional compareDigest (write only if the stored value still matches). Non-string values are JSON-encoded, as Shopify expects for json, list and number types. Exposed for testing.

Example

input ::shopify::metafields/build-metafield-input({
  ownerId: "gid://shopify/Product/1",
  namespace: "custom",
  key: "specs",
  type: "json",
  value: {size: "M"}
})
input.value  // "{\"size\":\"M\"}"

delete-metafields

fn (identifiers: Vec): Vec
fn (identifiers: Vec, opts: Map): Vec

Delete metafields by {ownerId, namespace, key} (metafieldsDelete). Returns the deleted identifiers (null entries for ones that didn't exist).

Example

::shopify::metafields/delete-metafields([
  {ownerId: "gid://shopify/Product/1", namespace: "custom", key: "care_guide"}
])

get-metafield

fn (owner-id: Str, namespace: Str, key: Str): Map?
fn (owner-id: Str, namespace: Str, key: Str, opts: Map): Map?

One metafield on an owner by namespace and key, or null when unset (or the owner doesn't exist).

Example

field ::shopify::metafields/get-metafield("gid://shopify/Product/1", "custom", "care_guide")
if(is-null(field), "unset", field.value)

list-metafields

fn (owner-id: Str): Map
fn (owner-id: Str, options: Map): Map
fn (owner-id: Str, options: Map, opts: Map): Map

One page of an owner's metafields. Options: namespace, first, after. Returns {nodes, pageInfo} (empty when the owner doesn't exist).

Example

page ::shopify::metafields/list-metafields("gid://shopify/Product/1", {namespace: "custom", first: 25})
map(page.nodes, %.key)

set-metafield

fn (owner-id: Str, namespace: Str, key: Str, value-type: Str, value: Any): Map
fn (owner-id: Str, namespace: Str, key: Str, value-type: Str, value: Any, opts: Map): Map

Set one metafield on an owner. Returns the saved metafield.

Example

saved ::shopify::metafields/set-metafield("gid://shopify/Product/1", "custom", "care_guide", "multi_line_text_field", "Hand wash cold.")
saved.id

set-metafields

fn (metafields: Vec): Vec
fn (metafields: Vec, opts: Map): Vec

Create or update up to 25 metafields atomically (metafieldsSet). Each entry is {ownerId, namespace, key, type, value, compareDigest?}; type may be omitted when a definition exists. Returns the saved metafields.

Example

::shopify::metafields/set-metafields([
  {ownerId: "gid://shopify/Product/1", namespace: "custom", key: "care_guide", type: "multi_line_text_field", value: "Hand wash cold."}
])