Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs when a user manually triggers the task

What you choose

  • Order tag to add
  • Ignore line items not fulfilled manually
  • Test mode
  • Run every 10 minutes
  • Run hourly

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

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 %}
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Order tag to add
ready-to-ship