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

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

Auto-untag customers when a certain product is refunded

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

Runs Occurs whenever a new refund is created without errors on an order, independent from the movement of money. Configuration includes product ids and customer tag to remove.

15-day free trial – unlimited tasks

Documentation

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

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, 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/refunds/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
product ids (number, array, required), customer tag to remove (required)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more