Auto-untag customers when a certain product is refunded, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-untag customers when a certain product is refunded

by Isaac Bowen (team@usemechanic.com)

Use this task to automatically remove a certain customer tag whenever a particular product is refunded, for that customer.

Runs when a refund is created. Configuration includes product ids and customer tag to remove.

15-day free trial – unlimited tasks

Documentation

This task is configured with one or more product IDs (learn how to find a product ID), and with the customer tag to remove when one of those products is refunded.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when a refund is created (shopify/refunds/create)
Options
product ids (number, array, required), customer tag to remove (required)
Script
{% if event.preview %}
  {% assign refund = hash %}
  {% assign refund["refund_line_items"] = array %}
  {% assign refund["refund_line_items"][0] = hash %}
  {% assign refund["refund_line_items"][0]["line_item"] = hash %}
  {% assign refund["refund_line_items"][0]["line_item"]["product_id"] = options.product_ids__number_array_required.first %}
  {% assign refund["order"] = hash %}
  {% assign refund["order"]["customer"] = hash %}
  {% assign refund["order"]["customer"]["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
  {% assign refund["order"]["customer"]["tags"] = options.customer_tag_to_remove__required %}
{% endif %}

{% assign refunded_product_ids = refund.refund_line_items | map: "line_item" | map: "product_id" %}

{% assign refund_qualifies = false %}

{% for product_id in options.product_ids__number_array_required %}
  {% if refunded_product_ids contains product_id %}
    {% assign refund_qualifies = true %}
    {% break %}
  {% endif %}
{% endfor %}

{% assign customer = refund.order.customer %}
{% assign customer_tags = refund.order.customer.tags | split: "," %}

{% if refund_qualifies and customer_tags contains options.customer_tag_to_remove__required %}
  {% action "shopify" %}
    mutation {
      tagsRemove(
        id: {{ customer.admin_graphql_api_id | json }}
        tags: {{ options.customer_tag_to_remove__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.