Skip to content

Commit

Permalink
Delete non-purchased Neon Sign Customiser app generated products olde…
Browse files Browse the repository at this point in the history
…r than X days (#277)

* new task! For an app that suggests Mechanic on a regular basies for deleting products that are auto-generated by their app.
  • Loading branch information
mattsodomsky authored Sep 7, 2023
1 parent ea6c9fa commit bf70cae
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Delete customer metafields in bulk](./delete-customer-metafields-in-bulk)
* [Delete draft orders older than 30 days](./delete-draft-orders-older-than-30-days)
* [Delete draft orders](./delete-draft-orders)
* [Delete non-purchased Neon Sign Customiser app generated products older than X days](./delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days)
* [Delete product or product variant metafields in bulk](./delete-product-or-product-variant-metafields-in-bulk)
* [Delete the oldest x products from a specific collection](./delete-the-oldest-x-products-from-a-specific-collection)
* [Delete variants having a metafield date that has passed](./delete-variants-having-a-metafield-date-that-has-passed)
Expand Down Expand Up @@ -717,6 +718,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Delete cancelled orders after 48 hours](./delete-cancelled-orders-after-48-hours)
* [Delete draft orders older than 30 days](./delete-draft-orders-older-than-30-days)
* [Delete draft orders](./delete-draft-orders)
* [Delete non-purchased Neon Sign Customiser app generated products older than X days](./delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days)
* [Delete the oldest x products from a specific collection](./delete-the-oldest-x-products-from-a-specific-collection)
* [Delete variants having a metafield date that has passed](./delete-variants-having-a-metafield-date-that-has-passed)

Expand Down Expand Up @@ -1322,6 +1324,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Copy product metafields to each product's tags](./copy-product-metafields-to-each-products-tags)
* [Create a product inventory CSV feed](./create-a-product-inventory-feed)
* [Delete all products](./delete-all-products)
* [Delete non-purchased Neon Sign Customiser app generated products older than X days](./delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days)
* [Delete product or product variant metafields in bulk](./delete-product-or-product-variant-metafields-in-bulk)
* [Delete the oldest x products from a specific collection](./delete-the-oldest-x-products-from-a-specific-collection)
* [Email a CSV export of products](./email-a-csv-export-of-products)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Delete non-purchased Neon Sign Customiser app generated products older than X days

Tags: Delete, Products

Use this task to automatically delete non-purchased products created by [Neon Sign Customiser](https://apps.shopify.com/neon-product-customiser). Set the number of days old products should be before Mechanic deletes a product.

* View in the task library: [tasks.mechanic.dev/delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days](https://tasks.mechanic.dev/delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days)
* Task JSON, for direct import: [task.json](../../tasks/delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days.json)
* Preview task code: [script.liquid](./script.liquid)

## Default options

```json
{
"products_must_be_x_number_of_days_old__number__required": "7",
"test_mode__boolean": true,
"live_mode__boolean": false,
"run_daily__boolean": false
}
```

[Learn about task options in Mechanic](https://learn.mechanic.dev/core/tasks/options)

## Subscriptions

```liquid
mechanic/user/trigger
{% if options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
```

[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)

## Documentation

Use this task to automatically delete non-purchased products created by [Neon Sign Customiser](https://apps.shopify.com/neon-product-customiser). Set the number of days old products should be before Mechanic deletes a product.

Tick the "Test mode" checkbox to verify which products will be deleted: after saving, use the "Run task" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode.

Once you're ready, uncheck the "Test mode" box and check the "Live mode" box, save the task, then use "Run task" to permanently delete all matching products.

Check the run daily option to have the task run at 12AM daily.


## Installing this task

Find this task [in the library at tasks.mechanic.dev](https://tasks.mechanic.dev/delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days), and use the "Try this task" button. Or, import [this task's JSON export](../../tasks/delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days.json) – see [Importing and exporting tasks](https://learn.mechanic.dev/core/tasks/import-and-export) to learn how imports work.

## Contributions

Found a bug? Got an improvement to add? Start here: [../../CONTRIBUTING.md](../../CONTRIBUTING.md).

## Task requests

Submit your [task requests](https://mechanic.canny.io/task-requests) for consideration by the Mechanic community, and they may be chosen for development and inclusion in the [task library](https://tasks.mechanic.dev/)!
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{% comment %}
Options ordering:
{{ options.products_must_be_x_number_of_days_old__number__required }}
{{ options.test_mode__boolean }}
{{ options.live_mode__boolean }}
{% endcomment %}

{% assign products_must_be_x_number_of_seconds_old = options.products_must_be_x_number_of_days_old__number__required | times: 24 | times: 60 | times: 60 %}

{% assign products_age_threshold_s = "now" | date: "%s" | minus: products_must_be_x_number_of_seconds_old %}

{% assign products_age_threshold_human = products_age_threshold_s | date: "%Y-%m-%d %H:%M %:z" %}

{% assign products_age_threshold_date = products_age_threshold_s | date: "%Y-%m-%d" %}

{% log %}{{ "Threshold for deleteing products: " | append: products_age_threshold_human | json }}{% endlog %}

{% assign product_ids_and_titles = hash %}

{% capture product_query -%}
-tag:npc_ordered product_type:'Custom Neon' created_at:<{{ products_age_threshold_date }}
{% endcapture %}

{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
products(
first: 250
after: {{ cursor | json }}
sortKey: TITLE
query: {{ product_query | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
}
}
}
}
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"products": {
"pageInfo": {
"hasNextPage": false
},
"edges": [
{
"node": {
"id": "gid://shopify/Product/1234567890",
"title": "[sample product]"
}
}
]
}
}
}
{% endcapture %}

{% assign result = result_json | parse_json %}
{% endif %}

{% for product_edge in result.data.products.edges %}
{% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}
{% endfor %}

{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% if options.test_mode__boolean and options.live_mode__boolean %}
{% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}
{% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean %}
{% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}
{% elsif options.live_mode__boolean %}
{% for keyval in product_ids_and_titles %}
{% action "shopify" %}
mutation {
productDelete(
input: {
id: {{ keyval[0] | json }}
}
) {
deletedProductId
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"docs": "Use this task to automatically delete non-purchased products created by [Neon Sign Customiser](https://apps.shopify.com/neon-product-customiser). Set the number of days old products should be before Mechanic deletes a product.\n\nTick the \"Test mode\" checkbox to verify which products will be deleted: after saving, use the \"Run task\" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode. \n\nOnce you're ready, uncheck the \"Test mode\" box and check the \"Live mode\" box, save the task, then use \"Run task\" to permanently delete all matching products.\n\nCheck the run daily option to have the task run at 12AM daily.\n",
"halt_action_run_sequence_on_error": false,
"name": "Delete non-purchased Neon Sign Customiser app generated products older than X days",
"online_store_javascript": null,
"options": {
"products_must_be_x_number_of_days_old__number__required": "7",
"test_mode__boolean": true,
"live_mode__boolean": false,
"run_daily__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% comment %}\n Options ordering:\n {{ options.products_must_be_x_number_of_days_old__number__required }} \n {{ options.test_mode__boolean }}\n {{ options.live_mode__boolean }} \n{% endcomment %}\n\n{% assign products_must_be_x_number_of_seconds_old = options.products_must_be_x_number_of_days_old__number__required | times: 24 | times: 60 | times: 60 %}\n\n{% assign products_age_threshold_s = \"now\" | date: \"%s\" | minus: products_must_be_x_number_of_seconds_old %}\n\n{% assign products_age_threshold_human = products_age_threshold_s | date: \"%Y-%m-%d %H:%M %:z\" %}\n\n{% assign products_age_threshold_date = products_age_threshold_s | date: \"%Y-%m-%d\" %}\n\n{% log %}{{ \"Threshold for deleteing products: \" | append: products_age_threshold_human | json }}{% endlog %}\n\n{% assign product_ids_and_titles = hash %}\n\n{% capture product_query -%}\n -tag:npc_ordered product_type:'Custom Neon' created_at:<{{ products_age_threshold_date }}\n{% endcapture %}\n\n{% assign cursor = nil %}\n{% for n in (0..100) %}\n {% capture query %}\n query {\n products(\n first: 250\n after: {{ cursor | json }}\n sortKey: TITLE\n query: {{ product_query | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n title\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 \"products\": {\n \"pageInfo\": {\n \"hasNextPage\": false\n },\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"title\": \"[sample product]\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for product_edge in result.data.products.edges %}\n {% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}\n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% if options.test_mode__boolean and options.live_mode__boolean %}\n {% error \"Please choose either test mode or live mode.\" %}\n{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}\n {% error \"Please choose either test mode or live mode.\" %}\n{% elsif options.test_mode__boolean %}\n {% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}\n{% elsif options.live_mode__boolean %}\n {% for keyval in product_ids_and_titles %}\n {% action \"shopify\" %}\n mutation {\n productDelete(\n input: {\n id: {{ keyval[0] | json }}\n }\n ) {\n deletedProductId\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}",
"subscriptions": ["mechanic/user/trigger"],
"subscriptions_template": "mechanic/user/trigger\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": ["Delete", "Products"]
}

0 comments on commit bf70cae

Please sign in to comment.