forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatically-publish-and-unpublish-on-a-monthly-cycle.json
26 lines (26 loc) · 4.37 KB
/
automatically-publish-and-unpublish-on-a-monthly-cycle.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
{
"docs": "Use this task to make a single product or page available for a limited window of time, on a monthly basis. Redirect your visitors to a specific page when that resource is unavailable.\n\nConfigure this task using the resource type (either \"product\" or \"page\"), a resource ID ([learn how to find this](https://help.usemechanic.com/faqs/how-do-i-find-an-id-for-a-product-collection-order-or-something-else)), the days of the month for publishing and unpublishing, and the URL you'd like to send visitors to when they land on the resource when it's unpublished.\r\n\r\nThis task will run automatically, at midnight in your store's local timezone. Run this task manually, using the \"Run task\" button, if you'd like to test your settings immediately. :)",
"halt_action_run_sequence_on_error": false,
"name": "Automatically publish and unpublish on a monthly cycle",
"online_store_javascript": "{% if options.redirect_url_when_resource_is_not_published != blank %}\n {% assign resource_collection = options.resource_type__required | append: \"s\" %}\n {% assign resource = shop[resource_collection][options.resource_id__number_required] %}\n\n if (document.title.match(/404 Not Found/) && window.location.pathname.match({{ \"/\" | append: options.resource_type__required | append: \"s/\" | append: resource.handle | json }})) {\n window.location.href = {{ options.redirect_url_when_resource_is_not_published | json }};\n }\n{% endif %}",
"options": {
"resource_type__required": "product",
"resource_id__number_required": null,
"day_of_the_month_to_publish_resource__number_required": null,
"day_of_the_month_to_unpublish_resource__number_required": null,
"redirect_url_when_resource_is_not_published": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.resource_type__required != \"product\" and options.resource_type__required != \"page\" %}\n {\"error\": \"Set 'Resource type' to either 'product' or 'page'.\"}\n{% endif %}\n\n{% assign dom = \"now\" | date: \"%-d\" | times: 1 %}\n{% assign resource_collection = options.resource_type__required | append: \"s\" %}\n{% assign resource = shop[resource_collection][options.resource_id__number_required] %}\n\n{% assign publish_dom = options.day_of_the_month_to_publish_resource__number_required %}\n{% assign unpublish_dom = options.day_of_the_month_to_unpublish_resource__number_required %}\n\n{% action \"echo\" resource_collection, resource.handle, resource.published_at, dom, publish_dom, unpublish_dom %}\n\n{% if publish_dom < unpublish_dom and publish_dom <= dom and dom < unpublish_dom %}\n {% assign should_be_published = true %}\n{% elsif unpublish_dom < publish_dom and publish_dom <= dom %}\n {% assign should_be_published = true %}\n{% elsif unpublish_dom < publish_dom and unpublish_dom <= dom and dom <= publish_dom %}\n {% assign should_be_published = false %}\n{% elsif publish_dom < unpublish_dom and unpublish_dom <= dom %}\n {% assign should_be_published = false %}\n{% elsif publish_dom < unpublish_dom and dom < publish_dom %}\n {% assign should_be_published = false %}\n{% endif %}\n\n{% action \"echo\" should_be_published %}\n\n{% if event.preview or should_be_published == true %}\n {% if resource.published_at != blank %}\n {\"log\": \"Resource is already published; nothing to do.\"}\n {% else %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\n {{ options.resource_type__required | json }},\n {{ resource.id | json }}\n ],\n {\n \"published\": true\n }\n ]\n }\n }\n {% endif %}\n{% elsif should_be_published == false %}\n {% if resource.published_at == blank %}\n {\"log\": \"Resource is already unpublished; nothing to do.\"}\n {% else %}\n {\n \"action\": {\n \"type\": \"shopify\",\n \"options\": [\n \"update\",\n [\n {{ options.resource_type__required | json }},\n {{ resource.id | json }}\n ],\n {\n \"published\": false\n }\n ]\n }\n }\n {% endif %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/scheduler/daily"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/scheduler/daily",
"tags": [
"Products",
"Publish",
"Unpublish"
]
}