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
29 lines (29 loc) · 5.28 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
{
"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 hourly or daily.",
"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": "{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n\n {% capture query %}\n query {\n publications(first: 250) {\n edges {\n node {\n id\n name\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign publications = array %}\n\n {% for publication_edge in result.data.publications.edges %}\n {% if options.sales_channel_names__required_array contains publication_edge.node.name %}\n {% assign publications[publications.size] = publication_edge.node %}\n {% endif %}\n {% endfor%}\n\n {% if event.preview != true and publications.size != options.sales_channel_names__required_array.size %}\n {% log publications_named: options.sales_channel_names__required_array, publications_available: result.data.publications.edges, publications_matched: publications %}\n {% error \"Unable to find all named publications. Double-check your task configuration.\" %}\n {% endif %} \n\n {% capture bulk_operation_query %}\n query {\n products (query: {{ options.only_publish_products_matching_this_query | json }}) {\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\n {% capture query %}\n query {\n publications(first: 250) {\n edges {\n node {\n id\n name\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign publications = array %}\n\n {% for publication_edge in result.data.publications.edges %}\n {% if options.sales_channel_names__required_array contains publication_edge.node.name %}\n {% assign publications[publications.size] = publication_edge.node %}\n {% endif %}\n {% endfor%}\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 {% 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 event.preview %}\n {% assign product_ids_and_publication_ids[0] = array %}\n {% assign product_ids_and_publication_ids[0] = \"gid://shopify/Product/1234567890\" %}\n {% assign product_ids_and_publication_ids[0] = \"gid://shopify/Publication/1234567890\" %}\n {% endif %}\n\n {% if options.test_mode__boolean and event.preview == false %}\n {% action \"echo\" %}\n {\n \"message\": \"Found {{ product_ids_and_publication_ids.size }} publishing actions\",\n \"product_ids_and_publication_ids\": {{ product_ids_and_publication_ids | json }}\n }\n {% endaction %}\n {% else %}\n {% for product_id_and_publication_id in product_ids_and_publication_ids %}\n {% action \"shopify\" %}\n mutation {\n publishablePublish(\n id: {{ product_id_and_publication_id[0] | json }}\n input: {\n publicationId: {{ product_id_and_publication_id[1] | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n \n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation\n{% if options.run_hourly__boolean %}mechanic/scheduler/hourly{% endif %}\n{% if options.run_daily__boolean %}mechanic/scheduler/daily{% endif %}",
"tags": [
"Bulk",
"Products",
"Sales Channel"
]
}