Auto-tag orders based on shipping method, with Mechanic.

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

Auto-tag orders based on shipping method

Quickly identify and sort orders based on their shipping method, using automatic order tags. This task supports auto-tagging incoming orders, and can be run manually to tag historical orders.

Runs Occurs whenever an order is created and Occurs when a user manually triggers the task. Configuration includes shipping method titles and tags and allow partial matches for shipping method titles.

15-day free trial – unlimited tasks

Documentation

Quickly identify and sort orders based on their shipping method, using automatic order tags. This task supports auto-tagging incoming orders, and can be run manually to tag historical orders.

To use this task, populate the "Shipping method titles and tags" list with shipping method titles on the left, and order tags to apply on the right. In Shopify, shipping method titles are configured as "rates"; you can find yours by navigating to Settings > Shipping, then looking at the rates within each shipping zone.

This task will run automatically on incoming orders. Use the "Run task" button to scan and tag older orders. To process orders older than 60 days, enable "Read all orders".

Enable "Allow partial matches for shipping method titles" for more flexibility. With this option enabled, Mechanic will look for the first value in the "Shipping method titles and tags" list that is found within the order's shipping method title, and apply the corresponding tag.

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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
shipping method titles and tags (keyval, required), allow partial matches for shipping method titles (boolean)
Code
{% if event.preview %}
  {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
    {% assign tag = keyval[1] %}
    {% break %}
  {% endfor %}

  {% capture mutation %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Order/1234567890"
        tags: {{ tag | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endcapture %}

  {% action "shopify" mutation %}
{% elsif event.topic contains "shopify/orders/" %}
  {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}
    {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
      {% if order.shipping_lines.first.title contains keyval[0] %}
        {% assign tag = keyval[1] %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% else %}
    {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order.shipping_lines.first.title] %}
  {% endif %}

  {% assign order_tags = order.tags | split: ", " %}

  {"log": {{ order.shipping_lines.first.title | json | prepend: "Shipping method title for this order: " | json }}}

  {% if tag == nil %}
    {"log": "No tag to add for this order; nothing to do."}
  {% elsif order_tags contains tag %}
    {"log": "Order is already tagged {{ tag | json }}; nothing to do."}
  {% else %}
    {% capture mutation %}
      mutation {
        tagsAdd(
          id: {{ order.admin_graphql_api_id | default: "gid://shopify/Order/1234567890" | json }}
          tags: {{ tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endcapture %}

    {% action "shopify" mutation %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign query_parts = array %}
  {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
    {% assign query_parts[query_parts.size] = keyval[1] | json | prepend: "-tag:" %}
  {% endfor %}
  {% assign query = query_parts | uniq | join: " AND " %}

  {"log": {{ query | json | prepend: "Searching for orders matching " | json }}}

  {% assign cursor = nil %}
  {% for n in (0..100) %}
    {% capture orders_query %}
      query {
        orders(
          first: 250
          after: {{ cursor | json }}
          
          {% comment %}
            As of 2019-07-10, negative tag specifiers can result in an
            empty set of pages, *and* hasNextPage being true. Until that's
            fixed, we disable the query, and just deal with having to go
            through all orders. :)

            query: {{ query | json }}
          {% endcomment %}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              tags
              shippingLine {
                title
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign orders_result = orders_query | shopify %}

    {% for edge in orders_result.data.orders.edges %}
      {% assign order_node = edge.node %}
      {% assign tag = nil %}

      {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}
        {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
          {% if order_node.shippingLine.title contains keyval[0] %}
            {% assign tag = keyval[1] %}
            {% break %}
          {% endif %}
        {% endfor %}
      {% else %}
        {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order_node.shippingLine.title] %}
      {% endif %}

      {% if tag %}
        {% unless order_node.tags contains tag %}
          {% capture mutation %}
            mutation {
              tagsAdd(
                id: {{ order_node.id | json }}
                tags: {{ tag | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            }
          {% endcapture %}

          {% action "shopify" mutation %}
        {% endunless %}
      {% endif %}
    {% endfor %}

    {% if orders_result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = orders_result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Shipping method titles and tags
{"Shipping Alpha"=>"alpha", "Shipping Beta"=>"beta", "Shipping Gamma"=>"gamma"}