forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivate-a-discount-when-iss-passes-overhead.json
22 lines (22 loc) · 6.09 KB
/
activate-a-discount-when-iss-passes-overhead.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"docs": "You know what's worth celebrating? SPACE. Use this task to activate a discount code every time the International Space Station passes over your store (or some other location of your choice), powered by Mechanic webhooks, IFTTT, and NASA. 🚀\n\nTo get set up, follow our guide on [Triggering tasks with IFTTT](https://help.usemechanic.com/tutorials/triggering-tasks-with-ifttt), with these modifications:\r\n\r\n1. When configuring your Mechanic webhook, use the event topic `user/space/iss`.\r\n2. When choosing an IFTTT service (for \"+this\"), search for and select the \"Space\" service.\r\n3. When choosing an IFTTT trigger, select \"ISS passes over a specific location\", and choose the location of your store. Or, whatever location you prefer. :)\r\n4. When configuring the webhook action, set the content type to \"application/json\", and set the body to the following code:\r\n\r\n `{ \"duration_seconds\": \"{{DurationSeconds}}\", \"location_map_url\": \"{{LocationMapUrl}}\" }`\r\n\r\nThat's it! :) Every time the ISS arrives at your location, this task will activate the discount code of your choice (re-activating it when it already exists, and creating it if it does not), setting it to expire when the ISS departs.",
"halt_action_run_sequence_on_error": false,
"name": "Activate a discount when ISS passes overhead",
"online_store_javascript": null,
"options": {
"discount_code__required": "SPAAAACE",
"discount_percentage__number_required": "20.0",
"notification_email_address__email": ""
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign discount_code_code = options.discount_code__required %}\n{% assign discount_code = shop.discount_codes[discount_code_code] %}\n{% assign price_rule = discount_code.price_rule %}\n\n{% if event.data._sync and event.preview != true %}\n {% if discount_code == nil %}\n {% if price_rule == nil %}\n {% for some_price_rule in shop.price_rules %}\n {% if some_price_rule.title == discount_code_code %}\n {% assign price_rule = some_price_rule %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% endif %}\n\n {% if price_rule != nil %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"create\",\n [\"price_rule\", {{ price_rule.id | json }}, \"discount_code\"],\n {\n \"code\": {{ discount_code_code | json }}\n }\n ]\n }\n }\n {% else %}\n {\"log\": \"Failed to find price rule; can't create discount code.\"}\n {% endif %}\n {% endif %}\n{% else %}\n {% assign starts_at = \"now\" %}\n {% assign ends_at = \"now\" | date: \"%s\" | plus: event.data.duration_seconds %}\n\n {% assign starts_at_day = starts_at | date: \"%d %b\" %}\n {% assign ends_at_day = ends_at | date: \"%d %b\" %}\n {% assign ends_same_day = true %}\n {% if starts_at_day != ends_at_day %}\n {% assign ends_same_day = false %}\n {% endif %}\n\n {% assign minutes = event.data.duration_seconds | divided_by: 60 | floor %}\n {% assign seconds = minutes | times: 60 | minus: event.data.duration_seconds | times: -1 %}\n\n {% if options.notification_email_address__email != blank %}\n {% capture email_subject %}The ISS has arrived! Discount {{ discount_code_code }} is now active{% endcapture %}\n {% capture email_body %}\n Hello,\n\n Mechanic has just activated the discount code <b>{{ discount_code_code }}</b> (for {{ options.discount_percentage__number_required }}% off), to celebrate the International Space Station arriving <a href=\"{{ event.data.location_map_url }}\">at your store</a>!\n\n The ISS will be overhead for {{ minutes }}m{{ seconds }}s, so the discount will be active from {% if ends_same_day %}{{ starts_at | date: \"%l:%M %p\" }} to {{ ends_at | date: \"%l:%M %p\" }} ({{ \"now\" | date: \"%d %b\" }}){% else %}{{ starts_at | date: \"%d %b %Y at %l:%M %p\" }}, to {{ ends_at | date: \"%d %b %Y at %l:%M %p\" }}{% endif %}. You can manage it from <a href=\"https://{{ shop.domain }}/admin/discounts?query={{ discount_code_code }}\">your Shopify admin area</a>.\n\n Thanks,\n Mechanic, for {{ shop.name }}\n {% endcapture %}\n\n {\n \"action\": {\n \"type\": \"email\",\n \"options\": {\n \"to\": {{ options.notification_email_address__email | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | unindent | strip | newline_to_br | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }}\n }\n }\n }\n {% endif %}\n\n {% if price_rule == blank %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"create\",\n \"price_rule\",\n {\n \"title\": {{ discount_code_code | json }},\n \"target_type\": \"line_item\",\n \"target_selection\": \"all\",\n \"allocation_method\": \"across\",\n \"value_type\": \"percentage\",\n \"value\": \"-{{ options.discount_percentage__number_required }}\",\n \"customer_selection\": \"all\",\n \"starts_at\": {{ starts_at | date: \"%FT%T%:z\" | json }},\n \"ends_at\": {{ ends_at | date: \"%FT%T%:z\" | json }}\n }\n ]\n }\n }\n\n {\n \"action\": {\n \"type\": \"event\",\n \"options\": {\n \"topic\": \"user/space/iss\",\n \"data\": {\n \"_sync\": true\n }\n }\n }\n }\n {% else %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\"price_rule\", {{ price_rule.id | json }}],\n {\n \"starts_at\": {{ starts_at | date: \"%FT%T%:z\" | json }},\n \"ends_at\": {{ ends_at | date: \"%FT%T%:z\" | json }}\n }\n ]\n }\n }\n {% endif %}\n{% endif %}",
"subscriptions": [
"user/space/iss"
],
"subscriptions_template": "user/space/iss",
"tags": [
"Discounts",
"Schedule"
]
}