forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-customers-when-another-tag-is-added.json
23 lines (23 loc) · 4 KB
/
auto-tag-customers-when-another-tag-is-added.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "Does exactly as it says. :) Optionally, use combinations of tags to require, and/or combinations of tags to add.\n\nConfigure this task with customer tags to watch for on the left, and associated tags to add on the right. (Feel free to use comma-delimited lists on either side of that mapping!) This task will run whenever a customer is created or updated, tagging as configured.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers when another tag is added",
"online_store_javascript": null,
"options": {
"tags_to_watch_for_and_tags_to_add__keyval_required": null,
"remove_tag_to_add_when_the_corresponding_tag_to_watch_for_is_removed__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {% assign customer = hash %}\n {% assign customer[\"admin_graphql_api_id\"] = \"gid://shopify/Customer/1234567890\" %}\n {% assign customer[\"tags\"] = options.tags_to_watch_for_and_tags_to_add__keyval_required.first.first %}\n{% endif %}\n\n{% assign existing_tags = customer.tags | split: \", \" %}\n{% assign deserved_tags = array %}\n\n{% assign tags_to_add = array %}\n{% assign tags_to_remove = array %}\n\n{% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}\n {% assign tags_to_watch_for = keyval[0] | replace: \", \", \",\" | split: \",\" %}\n {% assign has_tags_to_watch_for = true %}\n\n {% for a_tag_to_watch_for in tags_to_watch_for %}\n {% unless existing_tags contains a_tag_to_watch_for %}\n {% assign has_tags_to_watch_for = false %}\n {% endunless %}\n {% endfor %}\n\n {% unless has_tags_to_watch_for %}\n {% continue %}\n {% endunless %}\n\n {% assign tags = keyval[1] | replace: \", \", \",\" | split: \",\" %}\n {% assign deserved_tags = deserved_tags | concat: tags %}\n\n {% for tag in tags %}\n {% unless existing_tags contains tag %}\n {% assign tags_to_add[tags_to_add.size] = tag %}\n {% endunless %}\n {% endfor %}\n{% endfor %}\n\n{% if options.remove_tag_to_add_when_the_corresponding_tag_to_watch_for_is_removed__boolean %}\n {% assign existing_tags_plus_tags_to_add = existing_tags | concat: tags_to_add %}\n\n {% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}\n {% assign tags_to_watch_for = keyval[0] | replace: \", \", \",\" | split: \",\" %}\n {% assign has_tags_to_watch_for = true %}\n\n {% for a_tag_to_watch_for in tags_to_watch_for %}\n {% unless existing_tags_plus_tags_to_add contains a_tag_to_watch_for %}\n {% assign has_tags_to_watch_for = false %}\n {% endunless %}\n {% endfor %}\n\n {% if has_tags_to_watch_for %}\n {% continue %}\n {% endif %}\n\n {% assign tags = keyval[1] | replace: \", \", \",\" | split: \",\" %}\n\n {% for tag in tags %}\n {% unless existing_tags contains tag %}\n {% continue %}\n {% endunless %}\n\n {% if deserved_tags contains tag %}\n {% continue %}\n {% endif %}\n\n {% assign tags_to_remove[tags_to_remove.size] = tag %}\n {% endfor %}\n {% endfor %}\n{% endif %}\n\n{% if tags_to_add != empty or tags_to_remove != empty %}\n {% action \"shopify\" %}\n mutation {\n {% if tags_to_add != empty %}\n tagsAdd(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n\n {% if tags_to_remove != empty %}\n tagsRemove(\n id: {{ customer.admin_graphql_api_id | json }}\n tags: {{ tags_to_remove | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/customers/create",
"shopify/customers/update"
],
"subscriptions_template": "shopify/customers/create\nshopify/customers/update",
"tags": [
"Auto-Tag",
"Customers",
"Tag"
]
}