forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-customers-with-vendors-after-ordering.json
21 lines (21 loc) · 5.49 KB
/
auto-tag-customers-with-vendors-after-ordering.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"docs": "This task tags customers with the name of each vendor in their order, as soon as the order is created. Useful for keeping track of which vendors a customer is interested in. :)\n\nThis task will run for each new order that's created, applying product vendors as customer tags.\r\n\r\nRun this task manually to have Mechanic scan your entire customer base, and each customer's order history. **This may take some time!**\r\n\r\nTo ensure that Mechanic can access your complete history, [make sure \"Read all orders\" is enabled](https://help.usemechanic.com/tutorials/enabling-read_all_orders).",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers with vendors after ordering",
"online_store_javascript": null,
"options": {},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic == \"shopify/orders/create\" %}\n {% assign customer = order.customer.reload %}\n\n {% if event.preview %}\n {% capture order_json %}\n {\n \"customer\": {\n \"admin_graphql_api_id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": \"ACME\"\n },\n \"line_items\": [\n {\n \"vendor\": \"ABC Company\"\n },\n {\n \"vendor\": \"ACME\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign order = order_json | parse_json %}\n {% assign customer = order.customer %}\n {% endif %}\n\n {% assign customer_tags = customer.tags | split: \", \" %}\n {% assign vendors = order.line_items | map: \"vendor\" | uniq %}\n\n {% assign tags_to_add = array %}\n\n {% for vendor in vendors %}\n {% unless customer_tags contains vendor %}\n {% assign tags_to_add[tags_to_add.size] = vendor %}\n {% endunless%}\n {% endfor %}\n\n {% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\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 }\n {% endaction %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n customers {\n edges {\n node {\n __typename\n id\n tags\n orders {\n edges {\n node {\n __typename\n id\n lineItems {\n edges {\n node {\n __typename\n id\n vendor\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture bulkOperation_objects_jsonl %}\n {\"__typename\":\"Customer\",\"id\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\",\"tags\":[\"ACME\"]}\n {\"__typename\":\"Order\",\"id\":\"gid:\\/\\/shopify\\/Order\\/1234567890\",\"__parentId\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\"}\n {\"__typename\":\"LineItem\",\"id\":\"gid:\\/\\/shopify\\/LineItem\\/1234567890\",\"vendor\":\"ABC Company\",\"__parentId\":\"gid:\\/\\/shopify\\/Order\\/1234567890\"}\n {\"__typename\":\"LineItem\",\"id\":\"gid:\\/\\/shopify\\/LineItem\\/2345678901\",\"vendor\":\"ACME\",\"__parentId\":\"gid:\\/\\/shopify\\/Order\\/1234567890\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = bulkOperation_objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign customers = bulkOperation.objects | where: \"__typename\", \"Customer\" %}\n\n {% for customer in customers %}\n {% assign vendors = array %}\n {% assign tags_to_add = array %}\n\n {% assign customer_orders = bulkOperation.objects | where: \"__typename\", \"Order\" | where: \"__parentId\", customer.id %}\n\n {% for order in customer_orders %}\n {% assign order_vendors = bulkOperation.objects | where: \"__typename\", \"LineItem\" | where: \"__parentId\", order.id | map: \"vendor\" | uniq %}\n {% assign vendors = vendors | concat: order_vendors %}\n {% endfor %}\n\n {% for vendor in vendors %}\n {% unless customer.tags contains vendor %}\n {% assign tags_to_add[tags_to_add.size] = vendor %}\n {% endunless%}\n {% endfor %}\n\n {% assign tags_to_add = tags_to_add | uniq %}\n\n {% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/orders/create\r\nmechanic/user/trigger\r\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Customers",
"Vendor"
]
}