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.
Try this taskWhen it runs
- Occurs whenever a customer is updated
- Occurs whenever user/customers/expire_tax_exempt is triggered
What you choose
- Tax exempt tag
- Days before removing tax exempt status
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/customers/update
user/customers/expire_tax_exempt
How task subscriptions work
Option types and flags
- Tax exempt tag (required)
- Days before removing tax exempt status (number, required)
Task 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
}
}
metafieldsDelete(
metafields: [
{
ownerId: {{ customer.id | json }}
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
}
]
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
Mechanic Liquid reference
Default option values
- Tax exempt tag
tax-exempt-for-30-days- Days before removing tax exempt status
30