-
Notifications
You must be signed in to change notification settings - Fork 35
/
email-vendors-when-their-products-are-ordered.json
52 lines (52 loc) · 8.96 KB
/
email-vendors-when-their-products-are-ordered.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
"docs": "Use this task to notify vendors when items of theirs have been purchased, by providing this task with a list of vendor names, and the related email addresses.\n\nConfigure the \"Vendors and email addresses\" option using vendor names on the left, and their email addresses on the right. Vendor names are case-sensitive!\n\nCustomize the email subject and body to taste. Use the default email body as a reference for making sure that you only show relevant line items in each vendor's email message.\n\n**Important:** If you need additional product or variant data to be included in the email that is not available in the order webhook data, then you may modify the GraphQL query and line item assignments within the task code. Look in the task code for comments that will assist with this.",
"halt_action_run_sequence_on_error": false,
"name": "Email vendors when their products are ordered",
"online_store_javascript": null,
"options": {
"vendors_and_email_addresses__keyval_required": {
"Vendor X": "vendor-x@example.com"
},
"email_subject__required": "New order: {{ event.data.order.name }} from {{ shop.name }}",
"email_body__required_multiline": "{% assign order = event.data.order %}\n\n<p>Hello,</p>\n\n<p>A new order has arrived ({{ order.name }}) for the following items:</p>\n\n<ul>\n{% for line_item in order.line_items %}\n<li>{{ line_item.quantity }}x {% if line_item.sku %}{{ line_item.sku }} -{% endif %} {{ line_item.title }} {% if line_item.variant_title != blank %}({{ line_item.variant_title }}){% endif %}{% if line_item.variant_barcode != blank %} - [{{ line_item.variant_barcode }}]{% endif %}\n</li>\n{% endfor %}\n</ul>\n\n<p>The order is to be shipped to the following address:</p>\n\n<p>\n{% if order.shipping_address %}\n{{ order.shipping_address.name }}\n{% if order.shipping_address.company != blank %}<br>{{ order.shipping_address.company }}{% endif %}\n<br>{{ order.shipping_address.address1 }}\n{% if order.shipping_address.address2 != blank %}<br>{{ order.shipping_address.address2 }}{% endif %}\n<br>{{ order.shipping_address.city }}, {{ order.shipping_address.province }}\n<br>{{ order.shipping_address.zip }} {{ order.shipping_address.country_code }}\n{% else %}\n(missing shipping address)\n{% endif %}\n</p>\n\n<p>\nThanks,\n<br>{{ shop.name }}\n</p>\n"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [
{
"description": "Sample order data filtered by vendor",
"event_attributes": {
"data": {
"order": {
"line_items": [
{
"id": 1234567890,
"quantity": 5,
"sku": "ABC123",
"title": "Test line item",
"variant_barcode": "987654321",
"variant_title": "Test variant title"
}
],
"name": "#PREVIEW"
},
"vendor": "Vendor X",
"vendor_email": "vendor-x@example.com"
},
"topic": "user/orders/send_vendor_email"
}
}
],
"script": "{% assign vendors_and_email_addresses = options.vendors_and_email_addresses__keyval_required %}\n{% assign email_subject = options.email_subject__required %}\n{% assign email_body = options.email_body__required_multiline %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% comment %}\n -- preview event is for the order webhook data available to this task\n {% endcomment %}\n\n {% if event.preview %}\n {% capture order_json %}\n {\n \"line_items\": [\n {\n \"id\": 1234567890,\n \"title\": \"Test line item\",\n \"quantity\": 5,\n \"sku\": \"ABC123\",\n \"vendor\": {{ vendors_and_email_addresses.first.first | json }},\n \"variant_title\": \"Test variant title\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign order = order_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- get a list of vendors on this order\n {% endcomment %}\n\n {% assign order_vendors = order.line_items | map: \"vendor\" | uniq %}\n\n {% for vendor in order_vendors %}\n {% if vendor == blank %}\n {% continue %}\n {% endif %}\n\n {% comment %}\n -- check to see if this vendor has been configured in the task\n {% endcomment %}\n\n {% assign vendor_email = vendors_and_email_addresses[vendor] %}\n\n {% if vendor_email == blank %}\n {% log message: \"No email address found for vendor; skipping\", vendor: vendor %}\n {% continue %}\n {% endif %}\n\n {% comment %}\n -- filter the line items by vendor\n {% endcomment %}\n\n {% assign vendor_line_items = order.line_items | where: \"vendor\", vendor %}\n\n {% comment %}\n -- create a clone of the order data from the webhook so its line items can be augmented with additional data from a GraphQL query\n {% endcomment %}\n\n {% assign order_data = order | json | parse_json %}\n {% assign order_data[\"line_items\"] = array %}\n\n {% comment %}\n -- this GraphQL query shows how to get additional product or variant data that is not available in the order webhook\n -- adjust it and the subsequent line_item assignments as needed for the Liquid content of your email body\n -- product GraphQL fields - https://shopify.dev/docs/api/admin-graphql/latest/objects/Product#fields-connections\n -- variant GraphQL fields - https://shopify.dev/docs/api/admin-graphql/latest/objects/ProductVariant#fields-connections\n -- order webhook data available for use without a query - https://shopify.dev/docs/api/webhooks?reference=toml#list-of-topics-orders/create\n {% endcomment %}\n\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n lineItems(first: 250) {\n nodes {\n id\n # product {\n # additional fields if needed\n # }\n variant {\n barcode\n # additional fields if needed\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"lineItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/LineItem/1234567890\",\n \"variant\": {\n \"barcode\": \"987654321\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign graphql_order = result.data.order %}\n\n {% for line_item in vendor_line_items %}\n {% comment %}\n -- create a clone of this line item data from the order webhook, so additional data can be added\n {% endcomment %}\n\n {% assign line_item_data = line_item | json | parse_json %}\n\n {% comment %}\n -- the following line gives access to the GraphQL data queried above for this specific line item\n {% endcomment %}\n\n {% assign graphql_line_item = graphql_order.lineItems.nodes | where: \"id\", line_item.admin_graphql_api_id | first %}\n\n {% comment %}\n -- add any additional data your email needs from the GraphQL query (e.g. barcode, which is not available in the order webhook data)\n {% endcomment %}\n\n {% assign line_item_data[\"variant_barcode\"] = graphql_line_item.variant.barcode %}\n\n {% comment %}\n -- add the cloned and enhanced line item into the order data to be passed to the vendor email event\n {% endcomment %}\n\n {% assign order_data[\"line_items\"][forloop.index0] = line_item_data %}\n {% endfor %}\n\n {% comment %}\n -- call a custom event to send the email so Liquid tags used in the subject and body have access to the augmented order data (in event.data)\n {% endcomment %}\n\n {% action \"event\" %}\n {\n \"topic\": \"user/orders/send_vendor_email\",\n \"task_id\": {{ task.id | json }},\n \"data\": {\n \"vendor\": {{ vendor | json }},\n \"vendor_email\": {{ vendor_email | json }},\n \"order\": {{ order_data | json }}\n }\n }\n {% endaction %}\n {% endfor %}\n\n{% elsif event.topic == \"user/orders/send_vendor_email\" %}\n {% action \"email\" %}\n {\n \"to\": {{ event.data.vendor_email | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }}\n }\n {% endaction %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"user/orders/send_vendor_email"
],
"subscriptions_template": "shopify/orders/create\nuser/orders/send_vendor_email",
"tags": [
"Email",
"Orders",
"Products",
"Vendor"
]
}