Mechanic Get Mechanic
Task library

Send email notification when items are returned

Use this task to get an email alert with a list of the returned items whenever a return is marked as closed.

Try this task

When it runs

Occurs whenever a return is closed

What you choose

  • Email recipients
  • Include POS returns

Documentation

Use this task to get an email alert with a list of the returned items whenever a return is marked as closed.

Optionally, enable "Include POS returns" if you would like return notifications for the Shopify Point of Sale channel as well.

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/returns/close
How task subscriptions work

Option types and flags

Task code

{% assign email_recipients = options.email_recipients__array_required %}
{% assign include_pos_returns = options.include_pos_returns__boolean %}

{% if event.topic == "shopify/returns/close" %}
  {% if event.preview %}
    {% capture return_json %}
      {
        "id": 1234567890,
        "admin_graphql_api_id": "gid://shopify/Return/1234567890",
        "order_id": 1234567890,
        "status": "closed"
      }
    {% endcapture %}

    {% assign return = return_json | parse_json %}
  {% endif %}

  {% comment %}
    -- get return data from the order resource in case notifications for POS returns are enabled
  {% endcomment %}

  {% capture query %}
    query {
      order(id: {{ return.order_id | prepend: "gid://shopify/Order/" | json }}) {
        id
        name
        displayFinancialStatus
        displayFulfillmentStatus
        returnStatus
        sourceName
        returns(
          first: 1
          query: {{ return.id | prepend: "id:" | json }}
        ) {
          nodes {
            id
            name
            returnLineItems(first: 100) {
              nodes {
                ... on ReturnLineItem {
                  quantity
                  returnReason
                  fulfillmentLineItem {
                    lineItem {
                      name
                      sku
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "name": "#PREVIEW",
            "sourceName": "pos",
            "returns": {
              "nodes": [
                {
                  "id": "gid://shopify/Return/10560471288",
                  "name": "#PREVIEW-R1",
                  "returnLineItems": {
                    "nodes": [
                      {
                        "quantity": 1,
                        "returnReason": "STYLE",
                        "fulfillmentLineItem": {
                          "lineItem": {
                            "name": "Widget - Chartreuse",
                            "sku": "WDGT-CHRTRS"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

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

  {% if order.sourceName == "pos" %}
    {% unless include_pos_returns or event.preview %}
      {% log "This return was made via POS, and the option to include them for notifications is disabled." %}
      {% break %}
    {% endunless %}
  {% endif %}

  {% assign return = order.returns.nodes.first %}

  {% assign return_line_items = return.returnLineItems.nodes %}

  {% if return_line_items == blank %}
    {% log
      message: "No verified return line items were found on this return.",
      return: return
    %}
    {% break %}
  {% endif %}

  {% capture email_subject %}ALERT: New return {{ return.name }}{% endcapture %}

  {% if include_pos_returns and order.sourceName == "pos" %}
    {% assign email_subject = email_subject | append: " (via POS)" %}
  {% endif %}

  {% capture email_body -%}
    Returned items
    ==============
    {% for return_line_item in return_line_items %}
      Title: <strong>{{ return_line_item.fulfillmentLineItem.lineItem.name }}</strong>
      SKU: <strong>{{ return_line_item.fulfillmentLineItem.lineItem.sku }}</strong>
      Quantity: <strong>{{ return_line_item.quantity }}</strong>
      Reason: <strong>{{ return_line_item.returnReason }}</strong>
    {% endfor %}
  {% endcapture %}

  {% action "email" %}
    {
      "to": {{ email_recipients | json }},
      "subject": {{ email_subject | json }},
      "body": {{ email_body | newline_to_br | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Include POS returns
false