-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathemail-a-csv-export-of-products.json
22 lines (22 loc) · 2.6 KB
/
email-a-csv-export-of-products.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"docs": "This task demonstrates Mechanic's ability to compile a CSV export, and send it as an email attachment. In this example, we bundle up a simple export of product titles and IDs, and email it as a CSV attachment.",
"halt_action_run_sequence_on_error": false,
"name": "Email a CSV export of products",
"online_store_javascript": null,
"options": {
"email_recipient__email_required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "\n{% comment %}\n -- create the initial array of arrays expected by the csv filter, and add the header row with column titles to it\n{% endcomment %}\n\n{% assign header_row = array | push: \"Product ID\", \"Product Title\" %}\n{% assign rows = array | push: header_row %}\n\n{% assign cursor = nil %}\n{% assign products = array %}\n\n{% for n in (1..100) %}\n {% capture query %}\n query {\n products(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n legacyResourceId\n title\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"products\": {\n \"nodes\": [\n {\n \"legacyResourceId\": \"1234567890\",\n \"title\": \"Short sleeve t-shirt\"\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for product in result.data.products.nodes %}\n {% assign row = array | push: product.legacyResourceId, product.title %}\n {% assign rows = rows | push: row %} \n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% assign csv = rows | csv %}\n\n{% action \"email\" %}\n {\n \"to\": {{ options.email_recipient__email_required | json }},\n \"subject\": {{ \"Product ID export for \" | append: shop.name | json }},\n \"body\": \"Please see attached. :)\",\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }},\n \"attachments\": {\n \"export.csv\": {{ csv | json }}\n }\n }\n{% endaction %}\n\n{% log csv %}\n",
"subscriptions": [
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/user/trigger",
"tags": [
"CSV",
"Email",
"Export",
"Products"
]
}