Auto-cancel fulfillments when an order is tagged, with Mechanic.

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

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.

Runs Occurs whenever an order is updated. Configuration includes required order tag.

15-day free trial – unlimited tasks

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.

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
required order tag (required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more