Cancel fulfillments when an order is fully refunded, with Mechanic.

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

Cancel fulfillments when an order is fully refunded

This task monitors for new refunds. When a new refund changes an order's status to fully refunded, this task will cancel all fulfillments for that order. This task supports running manually, to cancel all fulfillments for refunded orders, across your store's order history.

Runs Occurs when a user manually triggers the task and Occurs whenever a new refund is created without errors on an order, independent from the movement of money. Configuration includes cancel pending fulfillments, cancel open fulfillments, and cancel successful fulfillments.

15-day free trial – unlimited tasks

Documentation

This task monitors for new refunds. When a new refund changes an order's status to fully refunded, this task will cancel all fulfillments for that order. This task supports running manually, to cancel all fulfillments for refunded orders, across your store's order history.

Run this task manually to cancel fulfillments for refunded orders, across your store's order history. (To process orders older than 60 days, enable "read all orders".)

This task will also monitor for new refunds. When a new refund changes an order's status to fully refunded, this task will cancel all fulfillments for that order.

Configure this task to choose which fulfillment statuses Mechanic will look for.

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
mechanic/user/trigger
shopify/refunds/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
cancel pending fulfillments (boolean), cancel open fulfillments (boolean), cancel successful fulfillments (boolean)
Code
{% assign fulfillment_statuses_to_cancel = array %}

{% if options.cancel_pending_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | push: "PENDING" %}
{% endif %}

{% if options.cancel_open_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | push: "OPEN" %}
{% endif %}

{% if options.cancel_successful_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | push: "SUCCESS" %}
{% endif %}

{% if fulfillment_statuses_to_cancel == blank %}
  {% error "Please select at least one fulfillment status for cancellation" %}

{% elsif event.preview %}
  {% action "shopify" %}
    mutation {
      fulfillmentCancel(id: "gid://shopify/Fulfillment/1234567890") {
        fulfillment {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}
  {% assign orders = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        orders(
          first: 250
          after: {{ cursor | json }}
          query: "financial_status:refunded"
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            name
            fulfillments {
              id
              status
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "orders": {
              "nodes": [
                {
                  "id": "gid://shopify/Order/1234567890",
                  "fulfillments": [
                    {
                      "id": "gid://shopify/Fulfillment/1234567890",
                      "status": "PENDING"
                    },
                    {
                      "id": "gid://shopify/Fulfillment/2345678901",
                      "status": "OPEN"
                    },
                    {
                      "id": "gid://shopify/Fulfillment/3456789012",
                      "status": "SUCCESS"
                    }
                  ]
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign orders = orders | concat: result.data.orders.nodes %}

    {% if result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = result.data.orders.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% for order in orders %}
    {% for fulfillment in order.fulfillments %}
      {% if fulfillment_statuses_to_cancel contains fulfillment.status %}
        {% action "shopify" %}
          mutation {
            fulfillmentCancel(id: {{ fulfillment.id | json }}) {
              fulfillment {
                name
                status
              }
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endfor %}
  {% endfor %}

{% elsif refund.order.financial_status == "refunded" %}
  {% for fulfillment in refund.order.fulfillments %}
    {% assign fulfillment_status = fulfillment.status | upcase %}

    {% if fulfillment_statuses_to_cancel contains fulfillment_status %}
      {% action "shopify" %}
        mutation {
          fulfillmentCancel(id: {{ fulfillment.admin_graphql_api_id | json }}) {
            fulfillment {
              name
              status
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Cancel pending fulfillments
true