Skip to content

Commit

Permalink
add preview exception block (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Jul 9, 2024
1 parent e6077c1 commit 7a72f6c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ This directory is built automatically. Each task's documentation is generated fr

### Publish

* [Add all products to a certain sales channel](./add-all-products-to-a-certain-sales-channel)
* [Advanced: Scheduled section publishing](./advanced-scheduled-section-publishing)
* [Auto create collections by metafield values](./auto-create-collections-by-metafield-values)
* [Auto publish products by tag](./auto-publish-products-by-tag)
Expand Down
2 changes: 1 addition & 1 deletion docs/add-all-products-to-a-certain-sales-channel/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add all products to a certain sales channel

Tags: Bulk, Products, Sales Channel
Tags: Bulk, Products, Publish, Sales Channel

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.

Expand Down
14 changes: 8 additions & 6 deletions docs/add-all-products-to-a-certain-sales-channel/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
{% endif %}

{% if publications.size != sales_channel_names.size %}
{% log
publications_named: sales_channel_names,
publications_available: result.data.publications.nodes,
publications_matched: publications
%}
{% error "Unable to find all configured publications. Double-check your task configuration." %}
{% unless event.preview %}
{% log
publications_named: sales_channel_names,
publications_available: result.data.publications.nodes,
publications_matched: publications
%}
{% error "Unable to find all configured publications. Double-check your task configuration." %}
{% endunless %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
Expand Down
3 changes: 2 additions & 1 deletion tasks/add-all-products-to-a-certain-sales-channel.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"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 {% 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{% 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",
"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"
Expand All @@ -24,6 +24,7 @@
"tags": [
"Bulk",
"Products",
"Publish",
"Sales Channel"
]
}

0 comments on commit 7a72f6c

Please sign in to comment.