forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-all-products-to-a-certain-sales-channel.json
30 lines (30 loc) · 5.12 KB
/
add-all-products-to-a-certain-sales-channel.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
30
{
"docs": "Does exactly as it says: this task will publish all products to the sales channel(s) of your choice, on demand. Optionally, filter products by a search query, or choose to have this task run hourly or daily.\n\nNote: this task comes with a \"test mode\", which is enabled by default. Run the task at least once in this mode to make sure it would publish the products you expect, and then disbale the test mode.",
"halt_action_run_sequence_on_error": false,
"name": "Add all products to a certain sales channel",
"online_store_javascript": null,
"options": {
"sales_channel_names__required_array": [
"Online Store"
],
"only_publish_products_matching_this_query": null,
"test_mode__boolean": true,
"run_hourly__boolean": null,
"run_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign sales_channel_names = options.sales_channel_names__required_array %}\n{% assign products_query = options.only_publish_products_matching_this_query %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% capture query %}\n query {\n publications(first: 250) {\n nodes {\n id\n name\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% assign publications = array %}\n\n{% for publication in result.data.publications.nodes %}\n {% if sales_channel_names contains publication.name %}\n {% assign publications[publications.size] = publication %}\n {% endif %}\n{% endfor%}\n\n{% if event.preview %}\n {% assign publications[0] = hash %}\n {% assign publications[0][\"id\"] = \"gid://shopify/Publication/1234567890\" %}\n {% assign publications[0][\"name\"] = sales_channel_names.first %}\n{% endif %}\n\n{% if publications.size != sales_channel_names.size %}\n {% unless event.preview %}\n {% log\n publications_named: sales_channel_names,\n publications_available: result.data.publications.nodes,\n publications_matched: publications\n %}\n {% error \"Unable to find all configured publications. Double-check your task configuration.\" %}\n {% endunless %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% capture bulk_operation_query %}\n query {\n products (\n query: {{ products_query | json }}\n ) {\n edges {\n node {\n __typename\n id\n {% for publication in publications %}\n publishedOnPublication{{ forloop.index }}: publishedOnPublication(\n publicationId: {{ publication.id | json }}\n )\n {% endfor %}\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\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture jsonl_string %}\n {\"__typename\":\"Product\",\"id\":\"gid://shopify/Product/1234567890\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = jsonl_string | parse_jsonl %}\n {% endif %}\n\n {% assign products = bulkOperation.objects | where: \"__typename\", \"Product\" %}\n {% assign product_ids_and_publication_ids = array %}\n\n {% for product in products %}\n {% for publication in publications %}\n {% assign published_key = \"publishedOnPublication\" | append: forloop.index %}\n\n {% unless product[published_key] %}\n {% assign pair = array %}\n {% assign pair[0] = product.id %}\n {% assign pair[1] = publication.id %}\n {% assign product_ids_and_publication_ids[product_ids_and_publication_ids.size] = pair %}\n {% endunless %}\n {% endfor %}\n {% endfor %}\n\n {% if test_mode %}\n {% log\n message: \"Test mode: publishing actions logged only.\",\n publishing_actions_count: product_ids_and_publication_ids.size,\n product_ids_and_publication_ids: product_ids_and_publication_ids\n %}\n\n {% else %}\n {% for keyval in product_ids_and_publication_ids %}\n {% action \"shopify\" %}\n mutation {\n publishablePublish(\n id: {{ keyval[0] | json }}\n input: {\n publicationId: {{ keyval[1] | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation\n{% if options.run_hourly__boolean %}\n mechanic/scheduler/hourly\n{% elsif options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": [
"Bulk",
"Products",
"Publish",
"Sales Channel"
]
}