-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathemail-customers-a-unique-discount-code-two-weeks-after-order-fulfillment.json
27 lines (27 loc) · 13.2 KB
/
email-customers-a-unique-discount-code-two-weeks-after-order-fulfillment.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
{
"docs": "This task generates a brand new discount code (copying configuration from another code, of your choice), and emails it to the customer, after waiting a configurable number of days after order fulfillment.\n\nConfigure this task by first creating a discount code in your store, representative of the discount configuration you'd like this task to use. Provide the task with this source discount's code – when it runs, the task will clone this discount's configuration, using a new and unique discount name.\n\nConfigure with a number of days to wait after fulfillment, or remove this value completely to have the discount generated and sent immediately upon fulfillment.\n\nUse the text \"DISCOUNT_CODE\" to have the generated discount code inserted into your email to the customer, in either the subject or body.",
"halt_action_run_sequence_on_error": false,
"name": "Email customers a unique discount code, two weeks after order fulfillment",
"online_store_javascript": null,
"options": {
"days_to_wait_after_order_fulfillment__number": 14,
"source_discount_code_for_cloning__required": null,
"override_source_with_this_discount_validitity_period_in_days__number": null,
"new_discount_code_prefix": null,
"email_subject__required": "Thanks for your order! We've attached your discount. :)",
"email_body__required_multiline": "Hello {{ order.customer.first_name | default: \"there\" }},\n\nThanks for your order ({{ order.name }})! You can use the discount code DISCOUNT_CODE on your next order.\n\nCheers,\n{{ shop.name }}",
"email_bcc__email": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n An opinion about option order:\n\n {{ options.days_to_wait_after_order_fulfillment__number }}\n {{ options.source_discount_code_for_cloning__required }}\n {{ options.override_source_with_this_discount_validitity_period_in_days__number }}\n {{ options.new_discount_code_prefix }}\n {{ options.email_subject__required }}\n {{ options.email_body__required_multiline }}\n {{ options.email_bcc__email }}\n{% endcomment %}\n\n{% unless event.preview %}\n {% assign order = order.reload %}\n{% endunless %}\n\n{% if event.preview != true and order.email == blank %}\n {% log \"This order has no email address. Skipping. :)\" %}\n{% else %}\n {% assign source_discount_code = options.source_discount_code_for_cloning__required %}\n\n {% assign new_discount_code = order.id | split: \"\" | reverse | join: \"\" | slice: 0, 4 | append: order.order_number | base64 | replace: \"=\", \"\" | upcase | prepend: options.new_discount_code_prefix %}\n\n {% capture query %}\n query {\n codeDiscountNodeByCode(code: {{ source_discount_code | json }}) {\n id\n codeDiscount {\n __typename\n ... on DiscountCodeBasic {\n title\n usageLimit\n startsAt\n endsAt\n appliesOncePerCustomer\n summary\n minimumRequirement {\n __typename\n ... on DiscountMinimumQuantity {\n greaterThanOrEqualToQuantity\n }\n ... on DiscountMinimumSubtotal {\n greaterThanOrEqualToSubtotal {\n amount\n currencyCode\n }\n }\n }\n customerGets {\n items {\n __typename\n ... on AllDiscountItems {\n allItems\n }\n ... on DiscountProducts {\n productVariants(first: 250) {\n edges {\n node {\n id\n }\n }\n }\n products(first: 250) {\n edges {\n node {\n id\n }\n }\n }\n }\n ... on DiscountCollections {\n collections(first: 250) {\n edges {\n node {\n id\n }\n }\n }\n }\n }\n value {\n __typename\n ... on DiscountAmount {\n amount {\n amount\n currencyCode\n }\n appliesOnEachItem\n }\n ... on DiscountOnQuantity {\n effect {\n ... on DiscountPercentage {\n percentage\n }\n }\n quantity {\n quantity\n }\n }\n ... on DiscountPercentage {\n percentage\n }\n }\n }\n customerSelection {\n __typename\n ... on DiscountCustomers {\n customers {\n id\n }\n }\n ... on DiscountCustomerAll {\n allCustomers\n }\n ... on DiscountCustomerSegments {\n segments {\n id\n }\n }\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 \"codeDiscountNodeByCode\": {\n \"id\": \"gid://shopify/DiscountCodeNode/1234567890\",\n \"codeDiscount\": {\n \"__typename\": \"DiscountCodeBasic\",\n \"title\": \"DISCOUNT\",\n \"usageLimit\": 1,\n \"startsAt\": \"2019-11-04T20:06:44Z\",\n \"endsAt\": \"2019-11-13T04:59:59Z\",\n \"appliesOncePerCustomer\": true,\n \"summary\": \"50% off No shipping • Minimum purchase of $5.00 • For Isaac Bowen • One use per customer\",\n \"minimumRequirement\": {\n \"__typename\": \"DiscountMinimumSubtotal\",\n \"greaterThanOrEqualToSubtotal\": {\n \"amount\": \"5.0\",\n \"currencyCode\": \"USD\"\n }\n },\n \"customerGets\": {\n \"items\": {\n \"__typename\": \"DiscountProducts\",\n \"productVariants\": {\n \"edges\": []\n },\n \"products\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Product/1234567890\"\n }\n }\n ]\n }\n },\n \"value\": {\n \"__typename\": \"DiscountPercentage\",\n \"percentage\": 0.5\n }\n },\n \"customerSelection\": {\n \"__typename\": \"DiscountCustomers\",\n \"customers\": [\n {\n \"id\": \"gid://shopify/Customer/1234567890\"\n }\n ]\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign discountNode = result.data.codeDiscountNodeByCode.codeDiscount %}\n\n {% log discount_summary: discountNode.summary %}\n\n {% if discountNode == nil %}\n {% error \"Source discount was missing. Check your task configuration, and ensure that a discount exists with the configured code.\" %}\n {% elsif discountNode.__typename != \"DiscountCodeBasic\" %}\n {% error \"This task only supports basic discounts. It does not support free shipping, or buy-one-get-one.\" %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n discountCodeBasicCreate(\n basicCodeDiscount: {\n title: {{ new_discount_code | json }}\n code: {{ new_discount_code | json }}\n {% if options.override_source_with_this_discount_validitity_period_in_days__number != blank %}\n startsAt: {{ \"now\" | date: \"%FT%TZ\", tz: \"UTC\" | json }}\n {% assign interval = 86400 | times: options.override_source_with_this_discount_validitity_period_in_days__number %}\n endsAt: {{ \"now\" | date: \"%s\" | plus: interval | date: \"%FT%TZ\", tz: \"UTC\" | json }}\n {% else %}\n startsAt: {{ discountNode.startsAt | json }}\n endsAt: {{ discountNode.endsAt | json }}\n {% endif %}\n usageLimit: {{ discountNode.usageLimit | json }}\n appliesOncePerCustomer: {{ discountNode.appliesOncePerCustomer | json }}\n {% if discountNode.minimumRequirement.__typename == \"DiscountMinimumQuantity\" %}\n minimumRequirement: {\n quantity: 0\n }\n {% elsif discountNode.minimumRequirement.__typename == \"DiscountMinimumSubtotal\" %}\n minimumRequirement: {\n subtotal: {\n greaterThanOrEqualToSubtotal: {{ discountNode.minimumRequirement.greaterThanOrEqualToSubtotal.amount | json }}\n }\n }\n {% endif %}\n customerGets: {\n items: {\n {% if discountNode.customerGets.items.__typename == \"AllDiscountItems\" %}\n all: {{ discountNode.customerGets.items.allItems | json }}\n {% elsif discountNode.customerGets.items.__typename == \"DiscountProducts\" %}\n products: {\n productsToAdd: {{ discountNode.customerGets.items.products.edges | map: \"node\" | map: \"id\" | json }}\n productVariantsToAdd: {{ discountNode.customerGets.items.productVariants.edges | map: \"node\" | map: \"id\" | json }}\n }\n {% elsif discountNode.customerGets.items.__typename == \"DiscountCollections\" %}\n collections: {\n add: {{ discountNode.customerGets.items.collections.edges | map: \"node\" | map: \"id\" | json }}\n }\n {% endif %}\n }\n value: {\n {% if discountNode.customerGets.value.__typename == \"DiscountAmount\" %}\n discountAmount: {\n amount: {{ discountNode.customerGets.value.amount.amount | json }}\n appliesOnEachItem: {{ discountNode.customerGets.value.appliesOnEachItem | json }}\n }\n {% elsif discountNode.customerGets.value.__typename == \"DiscountOnQuantity\" %}\n discountOnQuantity: {\n quantity: {{ discountNode.customerGets.value.quantity.quantity | json }}\n effect: {\n percentage: {{ discountNode.customerGets.value.effect.percentage | json }}\n }\n }\n {% elsif discountNode.customerGets.value.__typename == \"DiscountPercentage\" %}\n percentage: {{ discountNode.customerGets.value.percentage | json }}\n {% endif %}\n }\n }\n customerSelection: {\n {% if discountNode.customerSelection.__typename == \"DiscountCustomers\" %}\n customers: {\n add: {{ discountNode.customerSelection.customers | map: \"id\" | json }}\n }\n {% elsif discountNode.customerSelection.__typename == \"DiscountCustomerAll\" %}\n all: {{ discountNode.customerSelection.allCustomers | json }}\n {% elsif discountNode.customerSelection.__typename == \"DiscountCustomerSegments\" %}\n customerSegments: {\n add: {{ discountNode.customerSelection.segments | map: \"id\" | json }}\n }\n {% endif %}\n }\n }\n ) {\n codeDiscountNode {\n id\n codeDiscount {\n ... on DiscountCodeBasic {\n summary\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% action \"email\" %}\n {\n \"to\": {{ order.email | json }},\n \"subject\": {{ options.email_subject__required | replace: \"DISCOUNT_CODE\", new_discount_code | json }},\n \"body\": {{ options.email_body__required_multiline | replace: \"DISCOUNT_CODE\", new_discount_code | newline_to_br | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }},\n \"bcc\": {{ options.email_bcc__email | json }}\n }\n {% endaction %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/fulfilled+14.days"
],
"subscriptions_template": "shopify/orders/fulfilled{% if options.days_to_wait_after_order_fulfillment__number != blank %}+{{ options.days_to_wait_after_order_fulfillment__number }}.days{% endif %}",
"tags": [
"Customers",
"Discounts",
"Schedule"
]
}