Auto-tag orders that are ready to ship, with Mechanic.

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

Auto-tag orders that are ready to ship

Use this task to tag orders, as they are created, if every single line item is related to a variant that's in stock.

Runs Occurs whenever an order is created and Occurs when a user manually triggers the task. Configuration includes order tag to add, ignore line items not fulfilled manually, test mode, run every 10 minutes, and run hourly.

15-day free trial – unlimited tasks

Documentation

Use this task to tag orders, as they are created, if every single line item is related to a variant that's in stock.

Enable "Ignore line items not fulfilled manually" to skip line items that you do not fulfill yourself from within Shopify. (This means that orders that consist entirely of these line items will never be tagged by this task.)

Check either of the scheduled run options to have the task scan all unfulfilled, untagged orders on each run to see if they qualify to be tagged.

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

{% if options.run_every_10_minutes__boolean %}
  mechanic/scheduler/10min
{% elsif options.run_hourly__boolean %}
  mechanic/scheduler/hourly
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
order tag to add (required), ignore line items not fulfilled manually (boolean), test mode (boolean), run every 10 minutes (boolean), run hourly (boolean)
Code
{% assign orders = array %}

{% if event.topic contains "shopify/orders/" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        tags
        lineItems(first: 20) {
          nodes {
            variant {
              id
              displayName
              inventoryQuantity
              inventoryItem {
                id
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% unless result.data.order.tags contains options.order_tag_to_add__required %}
    {% assign orders[0] = result.data.order %}
  {% endunless %}

{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign cursor = nil %}

  {% for n in (0..200) %}
    {% capture query %}
      query {
        orders(
          first: 10
          after: {{ cursor | json }}
          query: {{ options.order_tag_to_add__required | json | prepend: "fulfillment_status:unshipped tag_not:" | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            name
            lineItems(first: 20) {
              nodes {
                variant {
                  id
                  displayName
                  inventoryQuantity
                  inventoryItem {
                    id
                  }
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% assign orders = result.data.orders.nodes | default: array | concat: orders %}

    {% if result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = result.data.orders.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if event.preview %}
  {% capture orders_json %}
    [
      {
        "id": "gid://shopify/Order/1234567890",
        "name": "#PREVIEW",
        "lineItems": {
          "nodes": [
            {
              "variant": {
                "id": "gid://shopify/ProductVariant/1234567890",
                "displayName": "ACME Widget - Alpha",
                "inventoryQuantity": 2,
                "inventoryItem": {
                  "id": "gid://shopify/inventoryItem/1234567890"
                }
              }
            }
          ]
        }
      }
    ]
  {% endcapture %}

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

{% for order in orders %}
  {% assign order_qualifies = nil %}

  {% for line_item in order.lineItems.nodes %}
    {% if line_item.variant == blank %}
      {% continue %}
    {% endif %}

    {% if options.ignore_line_items_not_fulfilled_manually__boolean %}
      {% capture query %}
        query {
          inventoryItem(id: {{ line_item.variant.inventoryItem.id | json }}) {
            inventoryLevels(first: 100) {
              nodes {
                location {
                  fulfillmentService {
                    handle
                    serviceName
                  }
                }
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% comment %}
        Note: no preview stub data needed here, as null values for the fulfillment services are literally the happy path :)
      {% endcomment %}

      {% assign fulfillment_services
        = result.data.inventoryItem.inventoryLevels.nodes
        | map: "location"
        | map: "fulfillmentService"
        | compact
      %}

      {% if fulfillment_services != blank %}
        {% log
          message: "This inventory item for this line item variant is configured to use at least one 3rd party fulfillment service'; skipping.",
          order_name: order.name,
          line_item: line_item,
          fulfillment_services: fulfillment_services
        %}
        {% continue %}
      {% endif  %}
    {% endif %}

    {% if order_qualifies == nil and line_item.variant.inventoryQuantity >= 0 %}
      {% assign order_qualifies = true %}

    {% elsif line_item.variant.inventoryQuantity < 0 %}
      {% assign order_qualifies = false %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if order_qualifies %}
    {% if options.test_mode__boolean %}
      {% log order_id_to_tag: order.id, order_name_to_tag: order.name %}

    {% else %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ order.id | json }}
            tags: {{ options.order_tag_to_add__required | json }}
          ) {
            node {
              ... on Order {
                id
                name
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Order tag to add
ready-to-ship