Send email notification when items are returned, with Mechanic.

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

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.

Runs Occurs whenever a return is closed. Configuration includes email recipients and include pos returns.

15-day free trial – unlimited tasks

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.

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/returns/close
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
email recipients (array, required) , include pos returns (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more