Mechanic Get Mechanic
Task library

Auto-fulfill orders when tagged

This task monitors order tags, and when a certain tag is added to an order, it auto-fulfills all unfulfilled items in the order. Optionally, you can choose to notify customers when their order is fulfilled by this task.

Try this task

When it runs

Occurs whenever an order is updated

What you choose

  • Order tag to watch for
  • Ignore if this order tag is found
  • Notify customer on fulfillment

Documentation

This task monitors order tags, and when a certain tag is added to an order, it auto-fulfills all unfulfilled items in the order. Optionally, you can choose to notify customers when their order is fulfilled by this task.

Note: If a qualifying order has open fulfillments assigned to multiple locations or having multiple delivery methods when this task fulfills the order, then the customer will receive multiple notifications if that option is enabled in the task.

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/updated
How task subscriptions work

Option types and flags

Task code

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      tags
      fulfillmentOrders(
        first: 20
        query: "status:open"
      ) {
        nodes {
          id
          assignedLocation {
            name
          }
          deliveryMethod {
            methodType
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "tags": {{ options.order_tag_to_watch_for__required | json }},
          "fulfillmentOrders": {
            "nodes": [
              {
                "id": "gid://shopify/FulfillmentOrder/1234567890",
                "assignedLocation": {
                  "name": "ACME Warehouse"
                },
                "deliveryMethod": {
                  "methodType": "SHIPPING"
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

{% assign order = result.data.order %}
{% assign fulfillment_orders = order.fulfillmentOrders.nodes %}

{% if fulfillment_orders != blank and order.tags contains options.order_tag_to_watch_for__required %}
  {% if options.ignore_if_this_order_tag_is_found != blank and order.tags contains options.ignore_if_this_order_tag_is_found %}
    {% log message: "Order had the tag to watch for, but it also had the ignore tag. Skipping.", order_tags: order.tags, task_options: options %}
    {% break %}
  {% endif %}

  {% comment %}
    Note: fulfillments cannot be created with fulfillment orders at different locations or delivery types; so separate them out
  {% endcomment %}

  {% assign fulfillment_order_ids_by_location_and_type = hash %}

  {% for fulfillment_order in fulfillment_orders %}
    {% assign location_and_type
      = fulfillment_order.assignedLocation.name
      | append: "|"
      | append: fulfillment_order.deliveryMethod.methodType
    %}
    {% assign fulfillment_order_ids_by_location_and_type[location_and_type]
      = fulfillment_order_ids_by_location_and_type[location_and_type]
      | default: array
      | push: fulfillment_order.id
    %}
  {% endfor %}

  {% for keyval in fulfillment_order_ids_by_location_and_type %}
    {% action "shopify" %}
      mutation {
        fulfillmentCreate(
          fulfillment: {
            lineItemsByFulfillmentOrder: [
              {% for fulfillment_order_id in keyval[1] %}
                { fulfillmentOrderId: {{ fulfillment_order_id | json }} }
              {% endfor %}
            ]
            notifyCustomer: {{ options.notify_customer_on_fulfillment__boolean | json }}
          }
        ) {
          fulfillment {
            id
            status
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference

Default option values

Notify customer on fulfillment
false