forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpublish-products-that-fall-below-a-rolling-sales-threshold.json
26 lines (26 loc) · 8.55 KB
/
unpublish-products-that-fall-below-a-rolling-sales-threshold.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"docs": "This task scans the last x days of orders, and unpublishes any products that haven't met your specified sales threshold during that time period, from whichever sales channel you select. By default, this task adds up the line item subtotals for each product (quantity times price), but it can also count by total quantity sold. This task only looks at products that were published before the time period being examined.\n\nThis task scans the last x days of orders, and unpublishes any products that haven't met your specified sales threshold during that time period. By default, this task adds up the line item subtotals for each product (quantity times price), but it can also count by total quantity sold. This task only looks at products that were published before the time period being examined.\r\n\r\nUse test mode to ensure that this task does what you expect, before running it for real. :)",
"halt_action_run_sequence_on_error": false,
"name": "Unpublish products that fall below a rolling sales threshold",
"online_store_javascript": null,
"options": {
"sales_channel_name__required": "Online Store",
"number_of_days_back_to_look__number_required": 30,
"minimum_sales_threshold_for_staying_published__number_required": null,
"use_total_quantity_instead_of_line_item_subtotals__boolean": false,
"test_mode__boolean": true,
"run_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n An opinion about option order:\n\n {{ options.sales_channel_name__required }}\n {{ options.number_of_days_back_to_look__number_required }}\n {{ options.minimum_sales_threshold_for_staying_published__number_required }}\n {{ options.use_total_quantity_instead_of_line_item_subtotals__boolean }}\n {{ options.test_mode__boolean }}\n {{ options.run_daily__boolean }}\n{% endcomment %}\n\n{% capture query %}\n query {\n publications(first: 250) {\n edges {\n node {\n id\n name\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% assign publications = result.data.publications.edges | map: \"node\" | where: \"name\", options.sales_channel_name__required | first %}\n{% assign publication = publications | where: \"name\", options.sales_channel_name__required | first %}\n\n{% if event.preview != true and publication == nil %}\n {\"log\": {{ publications | map: \"name\" | json }}}\n {\"error\": {{ options.sales_channel_name__required | prepend: \"No sales channel found called \" | json }}}\n{% endif %}\n\n{% assign now_s = \"now\" | date: \"%s\" | times: 1 %}\n{% assign time_interval_s = options.number_of_days_back_to_look__number_required | times: 24 | times: 60 | times: 60 %}\n{% assign starting_date_s = now_s | minus: time_interval_s %}\n{% assign starting_date = starting_date_s | date: \"%Y-%m-%d\" %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic == \"mechanic/scheduler/daily\" %}\n {% capture query %}\n query {\n orders(\n query: {{ \"processed_at:>=\" | append: starting_date | json }}\n ) {\n edges {\n node {\n id\n name\n lineItems {\n edges {\n node {\n id\n quantity\n originalTotalSet {\n shopMoney {\n amount\n }\n }\n product {\n id\n publishedOnPublication(\n publicationId: {{ publication.id | json }}\n )\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if bulkOperation.objectCount == 0 %}\n {\"error\": \"No orders were found in this date range.\"}\n {% endif %}\n\n {% assign sales_by_product_id = hash %}\n\n {% for object in bulkOperation.objects %}\n {% if object.id contains \"gid://shopify/LineItem/\" %}\n {% assign product = object.product %}\n {% if product.publishedOnPublication %}\n {% if options.use_total_quantity_instead_of_line_item_subtotals__boolean %}\n {% assign sales_by_product_id[product.id] = sales_by_product_id[product.id] | default: 0 | plus: object.quantity %}\n {% else %}\n {% assign sales_by_product_id[product.id] = sales_by_product_id[product.id] | default: 0.0 | plus: object.originalTotalSet.shopMoney.amount %}\n {% endif %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% assign cursor = nil %}\n {% assign published_product_ids = array %}\n\n {% for n in (0..100) %}\n {% capture query %}\n query {\n publication(id: {{ publication.id | json }}) {\n id\n productPublicationsV3(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n isPublished\n publishDate\n publishable {\n ... on Product {\n id\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign some_product_publications = result.data.publication.productPublicationsV3.edges | map: \"node\" %}\n {% for product_publication in some_product_publications %}\n {% if product_publication.isPublished == false %}\n {% continue %}\n {% endif %}\n\n {% assign publish_date_s = product_publication.publishDate | date: \"%s\" | times: 1 %}\n {% if publish_date_s > starting_date_s %}\n {% if options.test_mode__boolean %}\n {\"log\": \"Ignoring product {{ product_publication.publishable.id }}; it was published after our starting date threshold.\"}\n {% endif %}\n {% continue %}\n {% endif %}\n\n {% assign published_product_ids[published_product_ids.size] = product_publication.publishable.id %}\n {% endfor %}\n\n {% if result.data.publication.productPublicationsV3.pageInfo.hasNextPage %}\n {% assign cursor = result.data.publication.productPublicationsV3.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% assign product_ids_to_unpublish = array %}\n\n {% for published_product_id in published_product_ids %}\n {% assign sales = sales_by_product_id[published_product_id] | default: 0 %}\n\n {% if sales < options.minimum_sales_threshold_for_staying_published__number_required %}\n {% assign product_ids_to_unpublish[product_ids_to_unpublish.size]= published_product_id %}\n {% endif %}\n {% endfor %}\n\n {% if event.preview %}\n {% assign published_product_ids[0] = \"gid://shopify/Product/123457890\" %}\n {% assign product_ids_to_unpublish[0] = \"gid://shopify/Product/123457890\" %}\n {% endif %}\n\n {% if options.test_mode__boolean %}\n {% action \"echo\" %}\n {\n \"message\": \"Found {{ product_ids_to_unpublish.size }} products to unpublish, out of {{ published_product_ids.size }} total products on this sales channel\",\n \"published_product_ids\": {{ published_product_ids | json }},\n \"sales_by_product_id\": {{ sales_by_product_id | json }},\n \"product_ids_to_unpublish\": {{ product_ids_to_unpublish | json }}\n }\n {% endaction %}\n {% else %}\n {% for product_id_to_unpublish in product_ids_to_unpublish %}\n {% action \"shopify\" %}\n mutation {\n publishableUnpublish(\n id: {{ product_id_to_unpublish | json }}\n input: {\n publicationId: {{ publication.id | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}\n\nmechanic/shopify/bulk_operation",
"tags": [
"Products",
"Unpublish"
]
}