Mechanic Get Mechanic
Task library

Auto-tag orders by sales channel

Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for all orders.

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

Sales channel names and tags

Documentation

Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for all orders.

Please note: this task works by sales channel, and as such does not apply to orders that are created by an app. It's sometimes difficult to distinguish whether a third-party is using a sales channel or an app; if you're having trouble with this task, try its companion, Auto-tag orders by app.

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

shopify/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task code

{% assign sales_channel_names_and_tags = options.sales_channel_names_and_tags__keyval_required %}

{% assign sales_channel_names = sales_channel_names_and_tags | keys %}

{% comment %}
  -- get all of the sales channel names (i.e. publications aka app catalogs) in the shop
{% endcomment %}

{% capture query %}
  query {
    publications(
      first: 250
      catalogType:APP
    ) {
      nodes {
        id
        catalog {
          ... on AppCatalog {
            apps(first: 1) {
              nodes {
                title
              }
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "publications": {
          "nodes": [
            {
              "id": "gid://shopify/Publication/1234567890",
              "catalog": {
                "apps": {
                  "nodes": [
                    {
                      "title": {{ sales_channel_names.first | json }}
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% comment %}
  -- add the configured publication names into a hash for use in lookups
{% endcomment %}

{% assign publication_ids = array %}
{% assign publication_names_by_id = hash %}

{% for publication in result.data.publications.nodes %}
  {% assign publication_name = publication.catalog.apps.nodes.first.title %}

  {% if sales_channel_names contains publication_name %}
    {% assign publication_ids = publication_ids | push: publication.id %}
    {% assign publication_names_by_id[publication.id] = publication_name %}
  {% endif %}
{% endfor %}

{% if event.topic contains "shopify/orders/" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        tags
        publication {
          id
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign orders = array | push: result.data.order %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            __typename
            id
            name
            tags
            publication {
              id
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

  {% break %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% endif %}

{% if event.preview %}
  {% capture orders_json %}
    [
      {
        "id": "gid://shopify/Order/1234567890",
        "name": "#PREVIEW",
        "publication": {
          "id": {{ publication_ids.first | json }}
        }
      }
    ]
  {% endcapture %}

  {% assign orders = orders_json | parse_json %}
{% endif %}

{% comment %}
  -- process orders to see which should be tagged
{% endcomment %}

{% for order in orders %}
  {% assign order_publication_id = order.publication.id %}
  {% assign publication_name = publication_names_by_id[order_publication_id] %}

  {% if publication_name == blank %}
    {% log
      order_id: order.id,
      order_name: order.name,
      message: "No sales channel present for this order; it was probably created by an app (see https://usemechanic.com/task/auto-tag-orders-by-app)."
    %}
    {% continue %}
  {% endif %}

  {% assign tag_to_add = sales_channel_names_and_tags[publication_name] %}

  {% if tag_to_add == blank %}
    {% log
      order_id: order.id,
      order_name: order.name,
      message: "No tag configured for this channel. Skipping.",
      publication_name: publication_name,
      available_channels: available_channels
    %}

  {% elsif order.tags contains tag_to_add %}
    {% log
      order_id: order.id,
      order_name: order.name,
      message: "The order already has the tag for this channel. Skipping.",
      publication_name: publication_name,
      tag_to_add: tag_to_add
    %}

  {% else %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.id | json }}
          tags: {{ tag_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference

Default option values

Sales channel names and tags
{"Online Store" => "online-store", "Buy Button" => "buy-button"}