Mechanic Get Mechanic
Task library

Auto-cancel fulfillments when an order is tagged

This task runs when an order is updated. When the tag of your choice is found in the order's tag list, all of its fulfillments (if any) will be cancelled, and the aforementioned tag will be removed.

Try this task

When it runs

Occurs whenever an order is updated

What you choose

Required order tag

Documentation

This task runs when an order is updated. When the tag of your choice is found in the order's tag list, all of its fulfillments (if any) will be cancelled, and the aforementioned tag will be removed.

Notes:

  • If the configured tag is added to an order with no fulfillments, the tag will be auto-removed, regardless.
  • If an older order happens to be updated, and it already has the configured tag, this task will process the order as if the tag was just added.
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
      fulfillments {
        id
        status
      }
    }
  }  
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "tags": {{ options.required_order_tag__required | json }},
          "fulfillments": [
            {
              "id": "gid://shopify/Fulfillment/1234567890",
              "status": "SUCCESS"
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign order = result.data.order %}

{% if order.tags contains options.required_order_tag__required %}
  {% for fulfillment in order.fulfillments %}
    {% if fulfillment.status == "CANCELLED" %}
      {% continue %}
    {% endif %}

    {% action "shopify" %}
      mutation {
        fulfillmentCancel(id: {{ fulfillment.id | json }}) {
          fulfillment {
            id
            status
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}

  {% action "shopify" %}
    mutation {
      tagsRemove(
        id: {{ order.id | json }}
        tags: {{ options.required_order_tag__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference