Include metafields in Shopify webhook events
Include Shopify metafields in webhook event payloads, and optionally filter by metafield values, using Mechanic custom Shopify webhooks.
Most tasks should start with regular shopify/... subscriptions. If a task needs a known metafield as soon as a Shopify webhook arrives, a custom Shopify webhook can ask Shopify to include that metafield in the webhook payload before Mechanic receives it. Custom Shopify webhooks can also ask Shopify to filter deliveries by metafield data.
Use this when you know which metafield namespace and key you need, and you want to avoid a follow-up GraphQL lookup during the task run. If the task needs flexible metafield queries, related resources, or the freshest current state, use the Shopify action or GraphQL in Liquid instead.
Create the custom Shopify webhook
Open Settings, choose Custom Shopify webhooks, and create a webhook for the Shopify topic you need.
Name: Products tracked by Mechanic
Shopify topic: shopify/products/update
Mechanic topic: user/products/tracked_update
Filter: id:*id:* keeps every delivery while giving Shopify a non-empty filter. Use a narrower filter when you only want matching resources.
Optional: filter by the metafield
Use metafields.namespace, metafields.key, and metafields.value when Shopify should only send resources with a matching metafield.
Filter:
metafields.namespace:custom AND metafields.key:track_with_mechanic AND metafields.value:true
Include fields:
id
title
updated_at
metafields
Metafields:
custom.track_with_mechanicFor a filter that only checks whether the metafield exists, keep the namespace and key terms and use metafields.value:*.
Subscribe a task to the Mechanic topic
Tasks subscribe to the custom user/... topic, not to the original Shopify topic.
user/products/tracked_updateIn task code, Mechanic still builds the usual Shopify subject variable from the source topic. For shopify/products/update, use product.
{% assign tracked_by_mechanic = false %}
{% for metafield in product.metafields %}
{% if metafield.namespace == "custom" and metafield.key == "track_with_mechanic" %}
{% assign tracked_by_mechanic = metafield.value %}
{% endif %}
{% endfor %}
{% log
topic: event.topic,
shopify_topic: event.shopify_topic,
product_id: product.id,
tracked_by_mechanic: tracked_by_mechanic
%}Test with a real Shopify update
Preview mode is useful for task code, but it does not prove Shopify's webhook payload shape. Save the custom Shopify webhook, trigger a matching Shopify update, then inspect the resulting event in Mechanic.
Check:
event.topicis youruser/...topicevent.shopify_topicis the original Shopify topicevent.data.metafieldscontains the metafield Shopify delivered
If your webhook uses Include fields and you want every matching update to arrive, include updated_at. If you leave updated_at out, Shopify may skip a recent repeat delivery when the trimmed payload would be identical.
Related
Last updated
Was this helpful?