Auto-fulfill orders when tagged, with Mechanic.

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

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.

Runs Occurs whenever an order is updated. Configuration includes order tag to watch for, ignore if this order tag is found, and notify customer on fulfillment.

15-day free trial – unlimited tasks

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.

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/updated
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
order tag to watch for (required), ignore if this order tag is found, notify customer on fulfillment (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more