forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredit-customers-for-sample-orders.json
24 lines (24 loc) · 4.71 KB
/
credit-customers-for-sample-orders.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
{
"docs": "Offer free samples to your customers at a nominal fee, and offer them that value back as a discount on a future order. This task generates a discount code, good for the amount of the sample order, and emails it to the customer.\n\nThis task will watch for newly paid orders, having a total that matches the number you configure. When a matching order comes in, this task generates a discount code, and emails it to the customer. The code can only be used once, by the customer who placed the original order, and must be used on an order that at least the value of the original order.",
"halt_action_run_sequence_on_error": false,
"name": "Credit customers for sample orders",
"online_store_javascript": null,
"options": {
"sample_order_total__number_required": 2.5,
"email_subject__required": "Thank you for your order! ({{ order.name }})",
"email_body__multiline_required": "Hi {{ order.customer.first_name | default: \"there\" }},\n\nThank you for ordering a sample! Please use this discount code to apply this value to your next order:\n\nDISCOUNT_CODE\n\nThanks,\nThe team at {{ shop.name }}",
"number_of_hours_to_wait__number": null,
"number_of_days_before_discounts_expire__number": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.sample_order_total__number_required <= 0 %}\n {\"error\": \"The order value must be a positive number.\"}\n{% endif %}\n\n{% assign order_qualifies = true %}\n\n{% if order.financial_status != \"paid\" %}\n {% assign order_qualifies = false %}\n {\"log\": \"Order is not paid.\"}\n{% endif %}\n\n{% assign order_total_price_number = order.total_price | times: 1 %}\n{% if order_total_price_number != options.sample_order_total__number_required %}\n {% assign order_qualifies = false %}\n {\"log\": \"Order has a different price than {{ options.sample_order_total__number_required }}.\"}\n{% endif %}\n\n{% if order.customer == nil %}\n {% assign order_qualifies = false %}\n {\"log\": \"Order does not have a customer record.\"}\n{% endif %}\n\n{% if event.preview or order_qualifies %}\n {% assign discount_code = order.id | split: \"\" | reverse | join: \"\" | slice: 0, 4 | append: order.order_number | base64 | replace: \"=\", \"\" | upcase %}\n\n {% capture graphql_query %}\n mutation {\n priceRuleCreate(\n priceRule: {\n allocationMethod: ACROSS\n customerSelection: {\n customerIdsToAdd: [{{ order.customer.reload.admin_graphql_api_id | json }}]\n }\n target: LINE_ITEM\n title: {{ discount_code | json }}\n usageLimit: 1\n validityPeriod: {\n start: {{ \"now\" | date: \"%FT%T%:z\" | json }}\n {% if options.number_of_days_before_discounts_expire__number != blank %}\n {% assign expiry_days_s = 60 | times: 60 | times: 24 | times: options.number_of_days_before_discounts_expire__number %}\n end: {{ \"now\" | date: \"%s\" | plus: expiry_days_s | date: \"%FT%T%:z\" | json }}\n {% endif %}\n }\n value: {\n fixedAmountValue: {{ options.sample_order_total__number_required | times: -1 | json }}\n }\n prerequisiteSubtotalRange: {\n greaterThanOrEqualTo: {{ options.sample_order_total__number_required | json }}\n }\n itemEntitlements: {\n targetAllLineItems: true\n }\n }\n priceRuleDiscountCode: {\n code: {{ discount_code | json }}\n }\n ) {\n priceRule {\n id\n }\n priceRuleUserErrors {\n code\n field\n message\n }\n priceRuleDiscountCode {\n code\n id\n }\n }\n }\n {% endcapture %}\n\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": {{ graphql_query | json }}\n }\n }\n\n {% assign email_subject = options.email_subject__required | replace: 'DISCOUNT_CODE', discount_code %}\n {% assign email_body = options.email_body__multiline_required | replace: 'DISCOUNT_CODE', discount_code %}\n\n {\n \"action\": {\n \"type\": \"email\",\n \"options\": {\n \"to\": {{ order.email | json }},\n \"subject\": {{ email_subject | strip | json }},\n \"body\": {{ email_body | newline_to_br | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }}\n }\n }\n }\n{% endif %}",
"subscriptions": [
"shopify/orders/paid"
],
"subscriptions_template": "shopify/orders/paid{% if options.number_of_hours_to_wait__number != blank %}+{{ options.number_of_hours_to_wait__number }}.hours{% endif %}",
"tags": [
"Customers",
"Orders"
]
}