Auto-approve return requests, with Mechanic.

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

Auto-approve return requests

Use this task to auto-approve return requests, typically made by customers using self-serve from their account. Choose to limit the auto-approval to orders with any of a set of tags, products with any of a set of tags, or specific product categories or types. Optionally, configure email recipients to be notified when any auto-approval occurs.

Runs Occurs whenever a return is requested and Occurs when a Mechanic action is performed. Configuration includes limit to orders with any of these tags, limit to products with any of these tags, limit to these product categories or types, and notification email recipients.

15-day free trial – unlimited tasks

Documentation

Use this task to auto-approve return requests, typically made by customers using self-serve from their account. Choose to limit the auto-approval to orders with any of a set of tags, products with any of a set of tags, or specific product categories or types. Optionally, configure email recipients to be notified when any auto-approval occurs.

More info on Shopify self-serve returns here.

Important:
- Adding multiple limit conditions means the return request must meet ALL of the conditions in order to be auto-approved.
- More specifically, if any product conditions are configured, then ALL products on the return must meet those conditions.
- Return requests that are auto-approved cannot later be used to exchange items.
- Approval of return requests cannot be reverted; instead, the return request may be cancelled if needed.

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/request
mechanic/actions/perform
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
limit to orders with any of these tags (array), limit to products with any of these tags (array), limit to these product categories or types (array), notification email recipients (array)
Code
{% assign limit_order_tags = options.limit_to_orders_with_any_of_these_tags__array %}
{% assign limit_product_tags = options.limit_to_products_with_any_of_these_tags__array %}
{% assign limit_product_categories_or_types = options.limit_to_these_product_categories_or_types__array %}
{% assign notification_email_recipients = options.notification_email_recipients__array %}

{% if event.topic == "shopify/returns/request" %}
  {% comment %}
    -- get additional return and product data not available in the webhook
  {% endcomment %}

  {% capture query %}
    query {
      return(id: {{ return.admin_graphql_api_id | json }}) {
        id
        name
        status
        order {
          name
          tags
        }
        returnLineItems(first: 100) {
          nodes {
            ... on ReturnLineItem {
              fulfillmentLineItem {
                lineItem {
                  product {
                    category {
                      name
                    }
                    productType
                    tags
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "return": {
            "id": "gid://shopify/Return/1234567890",
            "name": "#PREVIEW-R1",
            "status": "REQUESTED",
            "order": {
              "name": "#PREVIEW",
              "tags": {{ limit_order_tags.first | json }}
            },
            "returnLineItems": {
              "nodes": [
                {
                  "fulfillmentLineItem": {
                    "lineItem": {
                      "product": {
                        "category": {
                          "name": {{ limit_product_categories_or_types.first | json }}
                        },
                        "productType": {{ limit_product_categories_or_types.first | json }},
                        "tags": {{ limit_product_tags.first | json }}
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign return = result.data.return %}
  {% assign order = return.order %}
  {% 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: result.data.return
    %}
    {% break %}
  {% endif %}

  {% assign return_products
    = return_line_items
    | map: "fulfillmentLineItem"
    | map: "lineItem"
    | map: "product"
  %}

  {% if return.status != "REQUESTED" %}
    {% log "This return request does not have a status of 'REQUESTED' and likely has already been acted on; skipping." %}
    {% break %}
  {% endif %}

  {% comment %}
    -- assume the return request will be approved unless it doesn't meet any configured tag, category, or type limits
  {% endcomment %}

  {% assign return_qualifies = true %}

  {% if limit_order_tags != blank %}
    {% comment %}
      -- make sure the order has one of the configured tags
    {% endcomment %}

    {% assign has_qualifying_order_tag = nil %}

    {% for limit_order_tag in limit_order_tags %}
      {% if order.tags contains limit_order_tag %}
        {% assign has_qualifying_order_tag = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% unless has_qualifying_order_tag %}
      {% assign return_qualifies = false %}
    {% endunless %}
  {% endif %}

  {% if limit_product_tags != blank %}
    {% comment %}
      -- make sure each returned product has one of the configured tags
    {% endcomment %}

    {% assign all_products_qualify = true %}

    {% for product in return_products %}
      {% assign product_qualifies = nil %}

      {% for limit_product_tag in limit_product_tags %}
        {% if product.tags contains limit_product_tag %}
          {% assign product_qualifies = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% unless product_qualifies %}
        {% assign all_products_qualify = false %}
        {% break %}
      {% endunless %}
    {% endfor %}

    {% unless all_products_qualify %}
      {% assign return_qualifies = false %}
    {% endunless %}
  {% endif %}

  {% if limit_product_categories_or_types != blank %}
    {% comment %}
      -- make sure each returned product is one of the configured categories or types
    {% endcomment %}

    {% assign all_products_qualify = true %}

    {% for product in return_products %}
      {% assign product_qualifies = nil %}

      {% for limit_product_category_or_type in limit_product_categories_or_types %}
        {% if product.productType == limit_product_category_or_type or product.category.name == limit_product_category_or_type %}
          {% assign product_qualifies = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% unless product_qualifies %}
        {% assign all_products_qualify = false %}
        {% break %}
      {% endunless %}
    {% endfor %}

    {% unless all_products_qualify %}
      {% assign return_qualifies = false %}
    {% endunless %}
  {% endif %}

  {% if return_qualifies %}
    {% action "shopify" %}
      mutation {
        returnApproveRequest(input: {id: {{ return.id | json }}}) {
          return {
            id
            name
            status
            order {
              legacyResourceId
              customer {
                displayName
              }
            }
          }
          userErrors {
            code
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "mechanic/actions/perform" %}
  {% unless action.type == "shopify" and action.run.ok and notification_email_recipients != blank %}
    {% break %}
  {% endunless %}

  {% comment %}
    -- if any notification recipients are configured, send them an email when a return request is auto-approved
  {% endcomment %}

  {% assign return = action.run.result.data.returnApproveRequest.return %}

  {%- capture email_subject -%}
    Return request {{ return.name }} was auto-approved
  {%- endcapture -%}

  {%- capture email_body -%}
    Return request {{ return.name }}, made by {{ return.order.customer.displayName }}, was auto-approved.

    Review the return details and take further action on the <a href="{{ shop.admin_url }}orders/{{ return.order.legacyResourceId }}">order admin page</a>.

    Thanks,
    - Mechanic, for {{ shop.name }}
  {%- endcapture -%}

  {% action "email" %}
    {
      "to": {{ notification_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