- "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{% comment %}\n -- get all of the sales channel names (i.e. publications aka app catalogs) in the shop\n{% endcomment %}\n\n{% capture query %}\n query {\n publications(\n first: 250\n catalogType:APP\n ) {\n nodes {\n id\n catalog {\n ... on AppCatalog {\n apps(first: 1) {\n nodes {\n title\n }\n }\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"publications\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Publication/1234567890\",\n \"catalog\": {\n \"apps\": {\n \"nodes\": [\n {\n \"title\": {{ sales_channel_names.first | json }}\n }\n ]\n }\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% comment %}\n -- save the publication IDs for all of the configured sales channel names\n{% endcomment %}\n\n{% assign publication_ids = array %}\n{% assign available_sales_channel_names = array %}\n\n{% for publication in result.data.publications.nodes %}\n {% assign publication_name = publication.catalog.apps.nodes.first.title %}\n\n {% assign available_sales_channel_names = available_sales_channel_names | push: publication_name %}\n\n {% if sales_channel_names contains publication_name %}\n {% assign publication_ids = publication_ids | push: publication.id %}\n {% endif %}\n{% endfor %}\n\n{% if publication_ids == blank %}\n {% error\n message: \"None of the sales channel configured in this task exist in the shop. Check the list of available channels and verify each configured channel exists.\",\n configured_sales_channel_names: sales_channel_names,\n available_sales_channel_names: available_sales_channel_names\n %}\n\n {% break %}\n\n{% elsif publication_ids.size != sales_channel_names.size %}\n {% comment %}\n -- using action error here so the task will continue with any other configured and matched sales channels\n {% endcomment %}\n\n {% action \"echo\"\n __error: \"One or more configured sales channel names do not match any of the publication names available in this shop.\",\n configured_sales_channel_names: sales_channel_names,\n available_sales_channel_names: available_sales_channel_names\n %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- get all active products in the shop; optionally filter the products with configured query\n {% endcomment %}\n\n {% unless products_query contains \"status:active\" %}\n {% assign products_query = products_query | append: \" status:active\" | strip %}\n {% endunless %}\n\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_id in publication_ids %}\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 {% comment %}\n -- process all products returned by the bulk op query\n {% endcomment %}\n\n {% for product in products %}\n {% for publication_id in publication_ids %}\n {% assign published_key = \"publishedOnPublication\" | append: forloop.index %}\n\n {% unless product[published_key] %}\n {% assign product_ids_and_publication_ids[product_ids_and_publication_ids.size] = array | push: product.id, publication_id %}\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 {% break %}\n {% endif %}\n\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",
0 commit comments