Demonstration: Auto-tag new orders, with scheduled reconciliation
This task illustrates Shopify's recommendation of implementing reconciliation, in the case of missing Shopify events. This implementation handles the rare case that Shopify fails to deliver a webhook (orders/create, in this example), by scanning for unprocessed orders every 15 minutes.
Try this taskWhen it runs
- Occurs whenever an order is created
- Occurs every 15 minutes
- Occurs when a user manually triggers the task
What you choose
- Run on order create
- Reconcile every 15 minutes
- Reconcile on manual run
Documentation
This task illustrates Shopify's recommendation of implementing reconciliation, in the case of missing Shopify events. This implementation handles the rare case that Shopify fails to deliver a webhook (orders/create, in this example), by scanning for unprocessed orders every 15 minutes.
To learn more about this kind of scenario, see https://learn.mechanic.dev/core/shopify/events/reconciling-missing-events.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
{% if options.run_on_order_create__boolean %}
shopify/orders/create
{% endif %}
{% if options.reconcile_every_15_minutes__boolean %}
mechanic/scheduler/15min
{% endif %}
{% if options.reconcile_on_manual_run__boolean %}
mechanic/user/trigger
{% endif %}
How task subscriptions work
Option types and flags
- Run on order create (boolean)
- Reconcile every 15 minutes (boolean)
- Reconcile on manual run (boolean)
Task code
{% assign order_processed_tag = "processed-by-autotagger" %}
{% assign order_ids_to_process = array %}
{% if event.topic contains "shopify/orders/" %}
{% if event.preview %}
{% assign order = hash %}
{% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% endif %}
{% assign order_tags = order.tags | split: ", " %}
{% if order_tags contains order_processed_tag %}
{% log message: "Order is already processed; skipping" %}
{% else %}
{% assign order_ids_to_process[order_ids_to_process.size] = order.admin_graphql_api_id %}
{% endif %}
{% elsif event.topic contains "mechanic/scheduler/" or event.topic == "mechanic/user/trigger" %}
{% assign cursor = nil %}
{% assign orders_query_parts = array %}
{% assign orders_query_parts[0] = order_processed_tag | json | prepend: "-tag:" %}
{% assign orders_query_parts[1] = "now - 15 minutes" | date: "%Y-%m-%dT%H:%M:%SZ", tz: "UTC" | json | prepend: "created_at:>=" %}
{% assign orders_query = orders_query_parts | join: " " %}
{% log orders_query: orders_query %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
after: {{ cursor | json }}
query: {{ orders_query | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
tags
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"orders": {
"edges": [
{
"node": {
"id": "gid://shopify/Order/1234567890",
"tags": []
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for order_edge in result.data.orders.edges %}
{% comment %}
Double-checking this, to ward against stale search results from the API.
{% endcomment %}
{% unless order_edge.node.tags contains order_processed_tag %}
{% assign order_ids_to_process[order_ids_to_process.size] = order_edge.node.id %}
{% endunless %}
{% endfor %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% for order_id_to_process in order_ids_to_process %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order_id_to_process | json }}
tags: [{{ order_processed_tag | json }}]
) {
node {
# retrieving some post-tagging data, to ease debugging
... on Order {
id
name
tags
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
Mechanic Liquid reference
Default option values
- Run on order create
true- Reconcile every 15 minutes
true- Reconcile on manual run
true