Auto-tag orders using product tags, with Mechanic.

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

Auto-tag orders using product tags

Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for. Can be run manually, to scan and tag historical orders.

Runs Occurs whenever an order is created, Occurs when a user manually triggers the task, Occurs when a bulk operation is completed, and Occurs when a user sends an order to Mechanic. Configuration includes only copy these tags and only copy tags having one of these prefixes.

15-day free trial – unlimited tasks

Documentation

Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for. Can be run manually, to scan and tag historical orders.

Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for.

Run this task manually to scan and tag all orders, in bulk.

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
shopify/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
mechanic/user/order
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
only copy these tags (array), only copy tags having one of these prefixes (array)
Code
{% assign only_copy_these_tags = options.only_copy_these_tags__array %}
{% assign only_copy_tags_having_one_of_these_prefixes = options.only_copy_tags_having_one_of_these_prefixes__array %}

{% comment %}
  -- check configured tags to make sure no inadvertent spaces are present
{% endcomment %}

{% for tag in only_copy_these_tags %}
  {% assign tag_check = tag | strip %}

  {% if tag_check == "" %}
    {% error "'Only copy these tags' contains an empty entry. Please correct to continue." %}
  {% endif %}
{% endfor %}

{% for tag in only_copy_tags_having_one_of_these_prefixes %}
  {% assign tag_check = tag | strip %}

  {% if tag_check == "" %}
    {% error "'Only copy tags having one of these prefixes' contains an empty entry. Please correct to continue." %}
  {% endif %}
{% endfor %}

{% assign order_ids_tags_and_product_tags = array %}

{% if event.topic == "shopify/orders/create" or event.topic == "mechanic/user/order" %}
  {% comment %}
    -- query the order, line items, products, and tags
  {% endcomment %}

  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        tags
        lineItems(first: 250) {
          nodes {
            product {
              tags
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "lineItems": {
              "nodes": [
                {
                  "product": {
                    "tags": [
                      "preview-tag",
                      {{ only_copy_these_tags.first | json }},
                      {{ only_copy_tags_having_one_of_these_prefixes.first | json }}
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign order = result.data.order %}

  {% comment %}
    -- save order ID and tags as the only item in the array for processing
  {% endcomment %}

  {% assign item = array %}
  {% assign item[0] = order.id %}
  {% assign item[1] = order.tags %}
  {% assign item[2] = order.lineItems.nodes | map: "product" | map: "tags" | uniq %}
  {% assign order_ids_tags_and_product_tags[0] = item %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- use bulk op to get all orders in the shop
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            __typename
            id
            tags
            lineItems {
              edges {
                node {
                  __typename
                  product {
                    tags
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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 objects_jsonl %}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","tags":[]}
      {"__typename":"LineItem","__parentId":"gid:\/\/shopify\/Order\/1234567890","product":{"tags":["preview-tag",{{ only_copy_these_tags.first | json }},{{ only_copy_tags_having_one_of_these_prefixes.first | json }}]}}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
  {% assign line_items = bulkOperation.objects | where: "__typename", "LineItem" %}

  {% comment %}
    -- save order IDs and tags in array for processing
  {% endcomment %}

  {% for order in orders %}
    {% assign order_product_tags
      = line_items
      | where: "__parentId", order.id
      | map: "product"
      | map: "tags"
      | uniq
    %}

    {% assign item = array %}
    {% assign item[0] = order.id %}
    {% assign item[1] = order.tags %}
    {% assign item[2] = order_product_tags %}
    {% assign order_ids_tags_and_product_tags[order_ids_tags_and_product_tags.size] = item %}
  {% endfor %}
{% endif %}

{% comment %}
  -- process items to see which tags to add to each order
{% endcomment %}

{% for item in order_ids_tags_and_product_tags %}
  {% assign order_id = item[0] %}
  {% assign order_tags = item[1] %}
  {% assign order_product_tags = item[2] %}

  {% assign tags_to_add = array %}

  {% for product_tag in order_product_tags %}
    {% if order_tags contains product_tag %}
      {% continue %}
    {% endif %}

    {% if only_copy_these_tags == blank and only_copy_tags_having_one_of_these_prefixes == blank %}
      {% assign tags_to_add = tags_to_add | push: product_tag %}
      {% continue %}
    {% endif %}

    {% for tag in only_copy_these_tags %}
      {% if tag == product_tag %}
        {% assign tags_to_add = tags_to_add | push: product_tag %}
      {% endif %}
    {% endfor %}

    {% comment %}
      -- make sure the prefix matches the beginning of the tag
    {% endcomment %}

    {% for tag in only_copy_tags_having_one_of_these_prefixes %}
      {% assign prefix_length = tag.size %}
      {% assign product_tag_substr = product_tag | slice: 0, prefix_length %}

      {% if tag == product_tag_substr %}
        {% assign tags_to_add = tags_to_add | push: product_tag %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% comment %}
    -- remove duplicate tags before adding to order
  {% endcomment %}

  {% assign tags_to_add = tags_to_add | compact | uniq %}

  {% if tags_to_add != blank %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more