Temporarily enable tax-exempt status when a customer is tagged with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Temporarily enable tax-exempt status when a customer is tagged

by Mechanic Team (team@usemechanic.com)

This task monitors customer tags, applying tax-exempt status when a certain tag is detected, and removing it (and the tag) after a certain amount of time. Useful when you need to regularly re-approve customers for tax exemption.

Runs when a customer is updated and when user/customers/expire_tax_exempt is triggered. Configuration includes tax exempt tag and days before removing tax exempt status.

15-day free trial – unlimited tasks

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 customer is updated (shopify/customers/update)
when user/customers/expire_tax_exempt is triggered (user/customers/expire_tax_exempt)
Options
tax exempt tag (required), days before removing tax exempt status (number, required)
Script
{% assign metafield_namespace = "mechanic" %}
{% assign metafield_key = "tax-exempt-expiry-scheduled" %}
{% assign expiry_event_topic = "user/customers/expire_tax_exempt" %}

{% if event.topic contains "shopify/customers/" %}
  {% assign customer_qualifies = false %}
  {% assign customer_tags = customer.tags | split: ", " %}
  {% if customer_tags contains options.tax_exempt_tag__required and customer.metafields[metafield_namespace][metafield_key] == nil %}
    {% assign customer_qualifies = true %}
  {% endif %}

  {% if event.preview or customer_qualifies %}
    {% assign expiry_interval_s = 60 | times: 60 | times: 24 | times: options.days_before_removing_tax_exempt_status__number_required %}
    {% assign expiry_time = "now" | date: "%s" | plus: expiry_interval_s | round %}

    {% action "event" %}
      {
        "topic": {{ expiry_event_topic | json }},
        "data": {
          "customer_id": {{ customer.id | json }},
          "customer_tag": {{ options.tax_exempt_tag__required | json }}
        },
        "run_at": {{ expiry_time | json }}
      }
    {% endaction %}

    {% action "shopify" %}
      [
        "update",
        ["customer", {{ customer.id | json }}],
        {
          "tax_exempt": true,
          "metafields": [
            {
              "namespace": {{ metafield_namespace | json }},
              "key": {{ metafield_key | json }},
              "value_type": "integer",
              "value": 1
            }
          ]
        }
      ]
    {% endaction %}
  {% endif %}
{% elsif event.topic == expiry_event_topic %}
  {% assign customer = shop.customers[event.data.customer_id] %}
  {% assign customer_qualifies = false %}
  {% assign customer_tags = customer.tags | split: ", " %}
  {% if customer_tags contains options.tax_exempt_tag__required and customer.metafields[metafield_namespace][metafield_key] != nil %}
    {% assign customer_qualifies = true %}
  {% endif %}

  {% if event.preview or customer_qualifies %}
    {% action "shopify" %}
      [
        "update",
        ["customer", {{ customer.id | json }}],
        {
          "tax_exempt": false,
          "tags": {{ customer.tags | remove_tag: options.tax_exempt_tag__required | json }}
        }
      ]
    {% endaction %}

    {% assign metafield = customer.metafields[metafield_namespace] | where: "key", metafield_key | first %}

    {% capture metafield_query %}
      mutation {
        metafieldDelete(
          input: {
            id: {{ metafield.admin_graphql_api_id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endcapture %}

    {% action "shopify" metafield_query %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tax exempt tag
tax-exempt