Auto-tag orders with chargeback or inquiry activity, with Mechanic.

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

Auto-tag orders with chargeback or inquiry activity

Stay on top of chargebacks and inquiries! Nightly or on-demand, this task will scan the last 60 days of your order records, and apply the tags of your choice to orders with chargeback or inquiry activity, based on what that activity looks like.

Runs Occurs when a user manually triggers the task and Occurs every day at midnight (in local time). Configuration includes tag for any, tag for accepted, tag for charge refunded, tag for lost, tag for needs response, tag for under review, and tag for won.

15-day free trial – unlimited tasks

Documentation

Stay on top of chargebacks and inquiries! Nightly or on-demand, this task will scan the last 60 days of your order records, and apply the tags of your choice to orders with chargeback or inquiry activity, based on what that activity looks like.

If you would like for the task to scan orders beyond 60 days, you can enable read all orders in your Mechanic settings.

Learn more about chargebacks in the Shopify documentation:

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
mechanic/user/trigger
mechanic/scheduler/daily
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag for any (required), tag for accepted (required), tag for charge refunded (required), tag for lost (required), tag for needs response (required), tag for under review (required), tag for won (required)
Code
{% assign tag_for_any = options.tag_for_any__required | strip  %}

{% assign statuses_and_tags = hash %}
{% assign statuses_and_tags["ACCEPTED"] = options.tag_for_accepted__required | strip %}
{% assign statuses_and_tags["CHARGE_REFUNDED"] = options.tag_for_charge_refunded__required | strip %}
{% assign statuses_and_tags["LOST"] = options.tag_for_lost__required | strip %}
{% assign statuses_and_tags["NEEDS_RESPONSE"] = options.tag_for_needs_response__required | strip %}
{% assign statuses_and_tags["UNDER_REVIEW"] = options.tag_for_under_review__required | strip %}
{% assign statuses_and_tags["WON"] = options.tag_for_won__required | strip %}

{% assign status_tags = statuses_and_tags | values %}

{% assign cursor = nil %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      orders (
        first: 250
        after: {{ cursor | json }}
        query:"chargeback_status:any"
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            disputes {
              initiatedAs
              status
            }
            tags
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "orders": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Order/1234567890",
                  "legacyResourceId": "1234567890",
                  "disputes": [
                    {
                      "initiatedAs": "CHARGEBACK",
                      "status": "NEEDS_RESPONSE"
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

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

  {% assign orders = result.data.orders.edges | map: "node"  %}

  {% for order in orders %}
    {% assign tags_should_have = array %}
    {% assign tags_to_add = array %}
    {% assign tags_to_remove = array %}

    {% assign tags_should_have = tags_should_have | push: tag_for_any %}

    {% assign dispute_statuses
      = order.disputes
      | where: "initiatedAs", "CHARGEBACK"
      | map: "status"
    %}

    {% for dispute_status in dispute_statuses %}
      {% assign tags_should_have = tags_should_have | push: statuses_and_tags[dispute_status] %}
    {% endfor %}

    {% for status_tag in status_tags %}
      {% unless tags_should_have contains status_tag %}
        {% if order.tags contains status_tag %}
          {% assign tags_to_remove = tags_to_remove | push: status_tag %}
        {% endif %}
      {% endunless %}
    {% endfor %}

    {% for tag_should_have in tags_should_have %}
      {% unless order.tags contains tag_should_have %}
        {% assign tags_to_add = tags_to_add | push: tag_should_have %}
      {% endunless %}
    {% endfor %}

    {% log
      order_id: order.legacyResourceId,
      dispute_statuses: dispute_statuses,
      order_tags: order.tags,
      tags_should_have: tags_should_have,
      tags_to_add: tags_to_add,
      tags_to_remove: tags_to_remove
    %}

    {% if tags_to_add != blank or tags_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          {% if tags_to_add != blank %}
            tagsAdd(
              id: {{ order.id | json }}
              tags: {{ tags_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
          {% if tags_to_remove != blank %}
            tagsRemove(
              id: {{ order.id | json }}
              tags: {{ tags_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}

  {% if result.data.orders.pageInfo.hasNextPage %}
    {% assign cursor = result.data.orders.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Tag for any
chargeback-any
Tag for accepted
chargeback-accepted
Tag for charge refunded
chargeback-refunded
Tag for lost
chargeback-lost
Tag for needs response
chargeback-needs-response
Tag for under review
chargeback-under-review
Tag for won
chargeback-won