Auto-tag orders using product tags with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Auto-tag orders using product tags

by Isaac Bowen (team@usemechanic.com)

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.

Runs when an order is created. Configuration includes copy these tags and copy tags having this prefix.

15-day free trial – unlimited tasks

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)
Options
copy these tags (array), copy tags having this prefix
Script
{% assign order_product_tags = order.line_items | map: "product" | map: "tags" | join: ", " %}
{% assign order_tags_to_save = order.tags %}

{% if options.copy_these_tags__array == blank and options.copy_tags_having_this_prefix == blank %}
  {% assign order_tags_to_save = order.tags | add_tags: order_product_tags %}
{% else %}
  {% assign order_product_tags_array = order_product_tags | split: ", " | uniq %}

  {% if options.copy_these_tags__array != blank %}
    {% for order_product_tag in order_product_tags_array %}
      {% if options.copy_these_tags__array contains order_product_tag %}
        {% assign order_tags_to_save = order_tags_to_save | add_tag: order_product_tag %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% if options.copy_tags_having_this_prefix != blank %}
    {% assign prefix_length = options.copy_tags_having_this_prefix.size %}
    {% for order_product_tag in order_product_tags_array %}
      {% assign order_product_tag_substr = order_product_tag | slice: 0, prefix_length %}
      {% if order_product_tag_substr == options.copy_tags_having_this_prefix %}
        {% assign order_tags_to_save = order_tags_to_save | add_tag: order_product_tag %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endif %}

{% if event.preview or order_tags_to_save != order.tags %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "order",
          {{ order.id | json }}
        ],
        {
          "tags": {{ order_tags_to_save | json }}
        }
      ]
    }
  }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.