-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathemail-customers-who-purchased-product.json
29 lines (29 loc) · 11.6 KB
/
email-customers-who-purchased-product.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
{
"docs": "Need to reach customers who made a specific purchase? This task scans your order history, looking for orders that match the conditions you specify. For each qualifying order, the task sends an email to the customer, optionally linking to the product(s) ordered.\n\nConfigure \"Search query for orders\" with the same search query you might use in the Shopify admin area. For example:\r\n\r\n* Use `processed_at:>=2019-01-01 processed_at:<2020-01-01` for every order in 2019\r\n* Use `financial_status:paid` for all paid orders\r\n* Use `fulfillment_status:unshipped` for all unshipped orders\r\n\r\nConfigure this task with product IDs and/or variant IDs, to have the task look for orders that contain any matching products and/or variants. [Learn how to find these IDs.](https://help.usemechanic.com/en/articles/2946120-how-do-i-find-an-id-for-a-product-collection-order-or-something-else)\r\n\r\nUse `ORDER_NAME` in the email subject or body, to insert the name of the order (e.g. \"#1234\"). Use `PRODUCT_TITLES` or `PRODUCT_TITLES_WITH_LINKS` to list all products in the order.\r\n\r\nUse \"Test mode\" to have Mechanic show you what emails it _would_ send, if test mode were disabled.\r\n\r\nThis task requires you to certify that the messages sent are directly related to customer activity. Our email provider, Postmark, does not allow bulk messaging. [Read more about their policy.](https://postmarkapp.com/blog/bulk-vs-transactional)",
"halt_action_run_sequence_on_error": false,
"name": "Email all customers who made a purchase",
"online_store_javascript": null,
"options": {
"search_query_for_orders": "financial_status:paid processed_at:>=2019-10-01",
"product_ids_to_look_for__number_array": null,
"variant_ids_to_look_for__number_array": null,
"email_subject__required": "A note about ORDER_NAME",
"email_body__required_multiline": "Hello,\n\nThanks for ordering PRODUCT_TITLES_WITH_LINKS. We appreciate it. :)\n\nCheers,\n{{ shop.name }}",
"add_this_tag_to_matching_orders": null,
"test_mode__boolean": true,
"i_certify_that_messages_sent_here_are_related_to_customer_activity__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Option order\n\n {{ options.search_query_for_orders }}\n {{ options.product_ids_to_look_for__number_array }}\n {{ options.variant_ids_to_look_for__number_array }}\n {{ options.email_subject__required }}\n {{ options.email_body__required_multiline }}\n {{ options.add_this_tag_to_matching_orders }}\n {{ options.test_mode__boolean }}\n {{ options.i_certify_that_messages_sent_here_are_related_to_customer_activity__boolean }}\n{% endcomment %}\n\n{% if event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n orders(\n query: {{ options.search_query_for_orders | json }}\n ) {\n edges {\n node {\n __typename\n id\n name\n email\n lineItems {\n edges {\n node {\n __typename\n id\n product {\n legacyResourceId\n title\n onlineStoreUrl\n }\n variant {\n legacyResourceId\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{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign line_items_by_order_id = hash %}\n\n {% assign orders = bulkOperation.objects | where: \"__typename\", \"Order\" %}\n {% assign qualifying_orders = array %}\n\n {% for object in bulkOperation.objects %}\n {% case object.__typename %}\n {% when \"LineItem\" %}\n {% assign order = object.__parent %}\n {% if line_items_by_order_id[order.id] == nil %}\n {% assign line_items_by_order_id[order.id] = array %}\n {% endif %}\n\n {% assign count = line_items_by_order_id[order.id].size %}\n {% assign line_items_by_order_id[order.id][count] = object %}\n {% endcase %}\n {% endfor %}\n\n {% for order in orders %}\n {% assign order_qualifies = true %}\n\n {% if order.email == blank %}\n {% assign order_qualifies = false %}\n {% endif %}\n\n {% if options.product_ids_to_look_for__number_array != blank %}\n {% assign product_ids_qualify = false %}\n {% for line_item in line_items_by_order_id[order.id] %}\n {% assign legacy_id_number = line_item.product.legacyResourceId | times: 1 %}\n {% if options.product_ids_to_look_for__number_array contains legacy_id_number %}\n {% assign product_ids_qualify = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless product_ids_qualify %}\n {% assign order_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if options.variant_ids_to_look_for__number_array != blank %}\n {% assign variant_ids_qualify = false %}\n {% for line_item in line_items_by_order_id[order.id] %}\n {% assign legacy_id_number = line_item.variant.legacyResourceId | times: 1 %}\n {% if options.variant_ids_to_look_for__number_array contains legacy_id_number %}\n {% assign variant_ids_qualify = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless variant_ids_qualify %}\n {% assign order_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if order_qualifies %}\n {% assign qualifying_orders[qualifying_orders.size] = order %}\n {% endif %}\n {% endfor %}\n\n {% assign test_mode_email_summaries = array %}\n\n {% if event.preview %}\n {% assign qualifying_orders[0] = hash %}\n {% assign qualifying_orders[0][\"id\"] = \"gid://shopify/Order/1234567890\" %}\n {% assign qualifying_orders[0][\"name\"] = \"#1234\" %}\n {% assign qualifying_orders[0][\"email\"] = \"[email protected]\" %}\n\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"] = array %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][0] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][0][\"product\"] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][0][\"product\"][\"title\"] = \"Short Sleeve T-Shirt\" %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][0][\"product\"][\"onlineStoreUrl\"] = \"https://\" | append: shop.domain %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][1] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][1][\"product\"] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][1][\"product\"][\"title\"] = \"Medium Sleeve T-Shirt\" %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][2] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][2][\"product\"] = hash %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][2][\"product\"][\"title\"] = \"Long Sleeve T-Shirt\" %}\n {% assign line_items_by_order_id[\"gid://shopify/Order/1234567890\"][2][\"product\"][\"onlineStoreUrl\"] = \"https://\" | append: shop.domain %}\n {% endif %}\n\n {% for order in qualifying_orders %}\n {% assign email_to = order.email %}\n {% assign email_subject = options.email_subject__required | replace: \"ORDER_NAME\", order.name %}\n {% assign email_body = options.email_body__required_multiline | strip | newline_to_br | replace: \"ORDER_NAME\", order.name %}\n\n {% assign line_items_with_products = line_items_by_order_id[order.id] | where: \"product\" %}\n\n {% assign line_item_summaries = array %}\n\n {% for line_item in line_items_with_products %}\n {% assign summary = line_item.product.title %}\n\n {% if line_item.product.onlineStoreUrl != blank %}\n {% assign summary = '<a href=\"' | append: line_item.product.onlineStoreUrl | append: '\">' | append: summary | append: '</a>' %}\n {% endif %}\n\n {% if forloop.last and forloop.index0 >= 2 %}\n {% assign summary = 'and ' | append: summary %}\n {% endif %}\n\n {% assign line_item_summaries[line_item_summaries.size] = summary %}\n {% endfor %}\n\n {% if line_item_summaries.size == 2 %}\n {% assign line_items_summary = line_item_summaries | join: \" and \" %}\n {% else %}\n {% assign line_items_summary = line_item_summaries | join: \", \" %}\n {% endif %}\n\n {% assign line_items_summary_without_links = line_items_summary | strip_html %}\n\n {% assign email_body = email_body | replace: \"PRODUCT_TITLES_WITH_LINKS\", line_items_summary %}\n {% assign email_body = email_body | replace: \"PRODUCT_TITLES\", line_items_summary_without_links %}\n\n {% if options.test_mode__boolean %}\n {% assign test_mode_email_summary = hash %}\n {% assign test_mode_email_summary[\"to\"] = email_to %}\n {% assign test_mode_email_summary[\"subject\"] = email_subject %}\n {% assign test_mode_email_summary[\"body\"] = email_body %}\n {% assign test_mode_email_summaries[test_mode_email_summaries.size] = test_mode_email_summary %}\n {% else %}\n {% action \"email\" %}\n {\n \"to\": {{ email_to | 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\n {% if options.add_this_tag_to_matching_orders != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ options.add_this_tag_to_matching_orders | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if options.test_mode__boolean %}\n {% action \"echo\" %}\n {\n \"message\": \"Found {{ qualifying_orders.size }} qualifying order(s)\",\n \"email_summaries\": {{ test_mode_email_summaries | json }}\n }\n {% endaction %}\n\n {% if event.preview and options.add_this_tag_to_matching_orders != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: \"gid://shopify/Order/1234567890\"\n tags: {{ options.add_this_tag_to_matching_orders | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n{% endif %}\n\n{% unless event.preview %}\n {% if options.test_mode__boolean == false and options.i_certify_that_messages_sent_here_are_related_to_customer_activity__boolean == false %}\n {\"error\": \"Our mail provider, Postmark, does not allow bulk messages that are not related to customer activity. For more information, see https://postmarkapp.com/blog/bulk-vs-transactional\"}\n {% endif %}\n{% endunless %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Customers",
"Email",
"Loyalty"
]
}