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

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

Temporarily enable tax-exempt status when a customer is tagged

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 Occurs whenever a customer is updated and Occurs whenever 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, 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/customers/update
user/customers/expire_tax_exempt
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tax exempt tag (required), days before removing tax exempt status (number, required)
Code
{% 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/" %}
  {% if event.preview %}
    {% capture customer_json %}
      {
        "admin_graphql_api_id": "gid://shopify/Customer/1234567890",
        "tags": {{ options.tax_exempt_tag__required | json }},
        "metafields": {
          {{ metafield_namespace | json }}: {
            {{ metafield_key | json }}: null
          }
        }
      }
    {% endcapture %}

    {% assign customer = customer_json | parse_json %}
  {% endif %}

  {% assign customer_qualifies = false %}
  {% assign customer_tags = customer.tags | split: ", " %}
  {% if customer_tags contains options.tax_exempt_tag__required %}
    {% if customer.metafields[metafield_namespace][metafield_key] != nil %}
      {% log "Customer already has their metafield set - skipping" %}
    {% else %}
      {% assign customer_qualifies = true %}
    {% endif %}
  {% endif %}

  {% if 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.admin_graphql_api_id | json }},
          "customer_tag": {{ options.tax_exempt_tag__required | json }}
        },
        "run_at": {{ expiry_time | json }}
      }
    {% endaction %}

    {% action "shopify" %}
      mutation {
        customerUpdate(
          input: {
            id: {{ customer.admin_graphql_api_id | json }}
            taxExempt: true
            metafields: [
              {
                namespace: {{ metafield_namespace | json }}
                key: {{ metafield_key | json }}
                type: "number_integer"
                value: "1"
              }
            ]
          }
        ) {
          customer {
            id
            taxExempt
            metafield(namespace: {{ metafield_namespace | json }}, key: {{ metafield_key | json }}) {
              id
              value
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% elsif event.topic == expiry_event_topic %}
  {% capture customer_query %}
    query {
      customer(id: {{ event.data.customer_id | json }}) {
        id
        tags
        metafield(namespace: {{ metafield_namespace | json }}, key: {{ metafield_key | json }}) {
          id
          value
        }
      }
    }
  {% endcapture %}

  {% assign customer_result = customer_query | shopify %}

  {% if event.preview %}
    {% capture customer_result_json %}
      {
        "data": {
          "customer": {
            "id": "gid://shopify/Customer/1234567890",
            "tags": [{{ options.tax_exempt_tag__required | json }}],
            "metafield": {
              "id": "gid://shopify/Metafield/1234567890",
              "value": "1"
            }
          }
        }
      }
    {% endcapture %}

    {% assign customer_result = customer_result_json | parse_json %}
  {% endif %}

  {% assign customer = customer_result.data.customer %}

  {% assign customer_qualifies = false %}
  {% if customer.tags contains options.tax_exempt_tag__required and customer.metafield.value != nil %}
    {% assign customer_qualifies = true %}
  {% endif %}

  {% if event.preview or customer_qualifies %}
    {% action "shopify" %}
      mutation {
        customerUpdate(
          input: {
            id: {{ customer.id | json }}
            taxExempt: false
          }
        ) {
          userErrors {
            field
            message
          }
        }

        tagsRemove(
          id: {{ customer.id | json }}
          tags: {{ options.tax_exempt_tag__required | json }}
        ) {
          node {
            ... on Customer {
              tags
            }
          }
          userErrors {
            field
            message
          }
        }

        metafieldDelete(
          input: {
            id: {{ customer_result.data.customer.metafield.id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Tax exempt tag
tax-exempt-for-30-days
Days before removing tax exempt status
30