- "script": "{% assign sales_channel_names = options.sales_channel_names__array_required %}\n{% assign product_tag = options.product_tag__required %}\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 contains \"shopify/products/\" %}\n {% capture query %}\n query {\n product(id: {{ product.admin_graphql_api_id | json }}) {\n id\n tags\n {% for publication_id in publication_ids %}\n publishedOnPublication{{ forloop.index }}: publishedOnPublication(\n publicationId: {{ publication_id | json }}\n )\n {% endfor %}\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign products = array | push: result.data.product %}\n\n{% elsif event.topic contains \"mechanic/user/trigger\" %}\n {% comment %}\n -- get all active products in the shop\n {% endcomment %}\n\n {% capture bulk_operation_query %}\n query {\n products (\n query: \"status:active\"\n ) {\n edges {\n node {\n __typename\n id\n tags\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 {% break %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign products = bulkOperation.objects | where: \"__typename\", \"Product\" %}\n{% endif %}\n\n{% if event.preview %}\n {% capture products_json %}\n [\n {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"tags\": {{ array | push: product_tag | json }}\n }\n ]\n {% endcapture %}\n\n {% assign products = products_json | parse_json %}\n{% endif %}\n\n{% comment %}\n -- process all products returned by bulk op query or the product that triggered the event\n{% endcomment %}\n\n{% for product in products %}\n {% assign mutations = array %}\n\n {% if product.tags contains product_tag %}\n {% assign should_be_published = true %}\n {% else %}\n {% assign should_be_published = false %}\n {% endif %}\n\n {% for publication_id in publication_ids %}\n {% assign published_key = \"publishedOnPublication\" | append: forloop.index %}\n\n {% if product[published_key] %}\n {% unless should_be_published %}\n {% capture mutation %}\n publishableUnpublish{{ publication_id | remove: \"gid://shopify/Publication/\" }}: publishableUnpublish(\n id: {{ product.id | json }}\n input: {\n publicationId: {{ publication_id | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations = mutations | push: mutation %}\n {% endunless %}\n\n {% elsif should_be_published %}\n {% capture mutation %}\n publishablePublish{{ publication_id | remove: \"gid://shopify/Publication/\" }}: publishablePublish(\n id: {{ product.id | json }}\n input: {\n publicationId: {{ publication_id | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations = mutations | push: mutation %}\n {% endif %}\n {% endfor %}\n\n {% if mutations != blank %}\n {% action \"shopify\" %}\n mutation {\n {{ mutations | join: newline }}\n }\n {% endaction %}\n {% endif %}\n{% endfor %}\n",
0 commit comments