Auto-tag orders by sales channel, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders by sales channel

by Isaac Bowen (team@usemechanic.com)

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 customers based on their historical orders.

Runs when an order is created, when a user triggers the task, and when a bulk operation finishes. Configuration includes sales channel names and tags.

15-day free trial – unlimited tasks

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 customers based on their historical orders.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
sales channel names and tags (keyval, required)
Script
{% assign orders = array %}

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

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "name": "#1234",
            "tags": [],
            "publication": {
              "name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign orders[orders.size] = result.data.order %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            __typename
            id
            name
            tags
            publication {
              name
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture bulkOperation_json %}
      {
        "objects": [
          {
            "__typename": "Order",
            "id": "gid://shopify/Order/1234567890",
            "name": "#1234",
            "tags": [],
            "publication": {
              "name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
            }
          }
        ]
      }
    {% endcapture %}

    {% assign bulkOperation = bulkOperation_json | parse_json %}
  {% endif %}

  {% assign orders = bulkOperation.objects %}
{% endif %}

{% for order in orders %}
  {% assign publication_name = order.publication.name %}
  {% if publication_name == blank %}
    {% continue %}
  {% endif %}

  {% assign tag_to_add = options.sales_channel_names_and_tags__keyval_required[publication_name] %}

  {% if tag_to_add == blank %}
    {% log order_id: order.id, order_name: order.name, message: "No tag found for this channel. Skipping.", publication_name: publication_name %}
  {% elsif result.data.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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Sales channel names and tags
{"Online Store"=>"online-store", "Buy Button"=>"buy-button"}