From 8fe787b0fd2121f2c5397b6230df7d5ca23cb340 Mon Sep 17 00:00:00 2001 From: Brad Hover Date: Mon, 8 Jul 2024 19:04:00 -0700 Subject: [PATCH] add preview exception block --- docs/README.md | 1 + .../README.md | 2 +- .../script.liquid | 14 ++++++++------ ...dd-all-products-to-a-certain-sales-channel.json | 3 ++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7c9d7de0..c09b864d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) diff --git a/docs/add-all-products-to-a-certain-sales-channel/README.md b/docs/add-all-products-to-a-certain-sales-channel/README.md index e351f4f0..52f788c4 100644 --- a/docs/add-all-products-to-a-certain-sales-channel/README.md +++ b/docs/add-all-products-to-a-certain-sales-channel/README.md @@ -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. diff --git a/docs/add-all-products-to-a-certain-sales-channel/script.liquid b/docs/add-all-products-to-a-certain-sales-channel/script.liquid index 9b8238e1..7776a935 100644 --- a/docs/add-all-products-to-a-certain-sales-channel/script.liquid +++ b/docs/add-all-products-to-a-certain-sales-channel/script.liquid @@ -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/" %} diff --git a/tasks/add-all-products-to-a-certain-sales-channel.json b/tasks/add-all-products-to-a-certain-sales-channel.json index e44bff08..50ee8534 100644 --- a/tasks/add-all-products-to-a-certain-sales-channel.json +++ b/tasks/add-all-products-to-a-certain-sales-channel.json @@ -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" @@ -24,6 +24,7 @@ "tags": [ "Bulk", "Products", + "Publish", "Sales Channel" ] }