Skip to content

Commit

Permalink
add staus:active filter to products query
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus committed Sep 17, 2024
1 parent 7a99f73 commit ffd9480
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/add-all-products-to-a-certain-sales-channel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.
Does exactly as it says: this task will publish all active 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.

* View in the task library: [tasks.mechanic.dev/add-all-products-to-a-certain-sales-channel](https://tasks.mechanic.dev/add-all-products-to-a-certain-sales-channel)
* Task JSON, for direct import: [task.json](../../tasks/add-all-products-to-a-certain-sales-channel.json)
Expand Down Expand Up @@ -40,7 +40,7 @@ mechanic/shopify/bulk_operation

## Documentation

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.
Does exactly as it says: this task will publish all active 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.

Note: this task comes with a "test mode", which is enabled by default. Run the task at least once in this mode to make sure it would publish the products you expect, and then disbale the test mode.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% unless products_query contains "status:active" %}
{% assign products_query = products_query | append: " status:active" | strip %}
{% endunless %}

{% capture bulk_operation_query %}
query {
products (
Expand Down
4 changes: 2 additions & 2 deletions tasks/add-all-products-to-a-certain-sales-channel.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"docs": "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.\n\nNote: this task comes with a \"test mode\", which is enabled by default. Run the task at least once in this mode to make sure it would publish the products you expect, and then disbale the test mode.",
"docs": "Does exactly as it says: this task will publish all active 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.\n\nNote: this task comes with a \"test mode\", which is enabled by default. Run the task at least once in this mode to make sure it would publish the products you expect, and then disbale the test mode.",
"halt_action_run_sequence_on_error": false,
"name": "Add all products to a certain sales channel",
"online_store_javascript": null,
Expand All @@ -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 {% 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",
"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 {% 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 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 Down

0 comments on commit ffd9480

Please sign in to comment.