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.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs whenever a new refund is created without errors on an order, independent from the movement of money
What you choose
- Cancel pending fulfillments
- Cancel open fulfillments
- Cancel successful fulfillments
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.
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
mechanic/user/trigger
shopify/refunds/create
How task subscriptions work
Option types and flags
- Cancel pending fulfillments (boolean)
- Cancel open fulfillments (boolean)
- Cancel successful fulfillments (boolean)
Task 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 %}
Mechanic Liquid reference
Default option values
- Cancel pending fulfillments
true- Cancel open fulfillments
false- Cancel successful fulfillments
false