Demonstration: Auto-tag new orders, with scheduled reconciliation, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever an order is created, Occurs every 15 minutes, and Occurs when a user manually triggers the task. Configuration includes run on order create, reconcile every 15 minutes, and reconcile on manual run.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
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 %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
run on order create (boolean), reconcile every 15 minutes (boolean), reconcile on manual run (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Run on order create
true
Reconcile every 15 minutes
true
Reconcile on manual run
true