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

Mechanic is an automation development platform for Shopify. :)

Cancel fulfillments when an order is fully refunded

by Isaac Bowen (team@usemechanic.com)

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 when a user triggers the task and when a refund is created. Configuration includes cancel pending fulfillments, cancel open fulfillments, and cancel successful fulfillments.

15-day free trial – unlimited tasks

Documentation

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, Gurus, 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.

Events
when a user triggers the task (mechanic/user/trigger)
when a refund is created (shopify/refunds/create)
Options
cancel pending fulfillments (boolean), cancel open fulfillments (boolean), cancel successful fulfillments (boolean)
Script
{% assign fulfillment_statuses_to_cancel = "" %}

{% if options.cancel_pending_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | add_tag: "pending" %}
{% endif %}

{% if options.cancel_open_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | add_tag: "open" %}
{% endif %}

{% if options.cancel_successful_fulfillments__boolean %}
  {% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | add_tag: "success" %}
{% endif %}

{% assign fulfillment_statuses_to_cancel = fulfillment_statuses_to_cancel | split: ", " %}

{% if fulfillment_statuses_to_cancel.size == 0 %}
  {"log": "Please select at least one fulfillment status for cancellation."}
{% elsif event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/orders/1234567890/fulfillments/1234567890/cancel.json",
        {}
      ]
    }
  }
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for order in shop.orders.any.refunded %}
    {% for fulfillment in order.fulfillments %}
      {% if fulfillment_statuses_to_cancel contains fulfillment.status %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "post",
              "/admin/orders/{{ order.id }}/fulfillments/{{ fulfillment.id }}/cancel.json",
              {}
            ]
          }
        }
      {% endif %}
    {% endfor %}
  {% endfor %}
{% elsif refund.order.financial_status == "refunded" %}
  {% for fulfillment in refund.order.fulfillments %}
    {% if fulfillment_statuses_to_cancel contains fulfillment.status %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "post",
            "/admin/orders/{{ refund.order.id }}/fulfillments/{{ fulfillment.id }}/cancel.json",
            {}
          ]
        }
      }
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Cancel pending fulfillments
true