Skip to content

Commit 48cf372

Browse files
committed
add preview checks
1 parent 1ee2e67 commit 48cf372

File tree

8 files changed

+43
-35
lines changed

8 files changed

+43
-35
lines changed

docs/auto-publish-products-by-tag/script.liquid

+11-9
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@
8383
{% break %}
8484

8585
{% elsif publication_ids.size != sales_channel_names.size %}
86-
{% comment %}
87-
-- using action error here so the task will continue with any other configured and matched sales channels
88-
{% endcomment %}
89-
90-
{% action "echo"
91-
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
92-
configured_sales_channel_names: sales_channel_names,
93-
available_sales_channel_names: available_sales_channel_names
94-
%}
86+
{% unless event.preview %}
87+
{% comment %}
88+
-- using action error here so the task will continue with any other configured and matched sales channels
89+
{% endcomment %}
90+
91+
{% action "echo"
92+
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
93+
configured_sales_channel_names: sales_channel_names,
94+
available_sales_channel_names: available_sales_channel_names
95+
%}
96+
{% endunless %}
9597
{% endif %}
9698

9799
{% if event.topic contains "shopify/products/" %}

docs/sync-inventory-for-shared-skus/script.liquid

+6-4
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@
155155
{% endfor %}
156156

157157
{% if unmatched_skus != blank %}
158-
{% action "echo"
159-
__error: "One or more configured SKUs were not matched in this shop. Remove or correct these entries in the task configuration.",
160-
unmatched_skus: unmatched_skus
161-
%}
158+
{% unless event.preview %}
159+
{% action "echo"
160+
__error: "One or more configured SKUs were not matched in this shop. Remove or correct these entries in the task configuration.",
161+
unmatched_skus: unmatched_skus
162+
%}
163+
{% endunless %}
162164
{% endif %}
163165

164166
{% log variants_by_sku: variants_by_sku %}

docs/unpublish-products-that-have-been-out-of-stock-for-x-days/script.liquid

+11-9
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,17 @@
234234
{% break %}
235235

236236
{% elsif publication_ids.size != sales_channel_names.size %}
237-
{% comment %}
238-
-- using action error here so the task will continue with any other configured and matched sales channels
239-
{% endcomment %}
240-
241-
{% action "echo"
242-
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
243-
configured_sales_channel_names: sales_channel_names,
244-
available_sales_channel_names: available_sales_channel_names
245-
%}
237+
{% unless event.preview %}
238+
{% comment %}
239+
-- using action error here so the task will continue with any other configured and matched sales channels
240+
{% endcomment %}
241+
242+
{% action "echo"
243+
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
244+
configured_sales_channel_names: sales_channel_names,
245+
available_sales_channel_names: available_sales_channel_names
246+
%}
247+
{% endunless %}
246248
{% endif %}
247249

248250
{% comment %}

docs/unpublish-products-when-tagged/script.liquid

+11-9
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@
8484
{% break %}
8585

8686
{% elsif publication_ids.size != sales_channel_names.size %}
87-
{% comment %}
88-
-- using action error here so the task will continue with any other configured and matched sales channels
89-
{% endcomment %}
90-
91-
{% action "echo"
92-
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
93-
configured_sales_channel_names: sales_channel_names,
94-
available_sales_channel_names: available_sales_channel_names
95-
%}
87+
{% unless event.preview %}
88+
{% comment %}
89+
-- using action error here so the task will continue with any other configured and matched sales channels
90+
{% endcomment %}
91+
92+
{% action "echo"
93+
__error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
94+
configured_sales_channel_names: sales_channel_names,
95+
available_sales_channel_names: available_sales_channel_names
96+
%}
97+
{% endunless %}
9698
{% endif %}
9799

98100
{% comment %}

tasks/auto-publish-products-by-tag.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"order_status_javascript": null,
1111
"perform_action_runs_in_sequence": false,
12-
"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",
12+
"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 {% unless event.preview %}\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 {% endunless %}\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",
1313
"subscriptions": [
1414
"shopify/products/create",
1515
"shopify/products/update",

0 commit comments

Comments
 (0)