Mechanic is a development and ecommerce automation platform for Shopify. :)
Do you sell tickets to recurring events? Do you use a new product variant for each one? Use this task to regularly scan your catalog for product variants that have a date metafield, and to delete variants when their date has passed.
Runs Occurs when a user manually triggers the task, Occurs every day at midnight (in local time), and Occurs when a bulk operation is completed. Configuration includes date metafield namespace and date metafield key.
Do you sell tickets to recurring events? Do you use a new product variant for each one? Use this task to regularly scan your catalog for product variants that have a date metafield, and to delete variants when their date has passed.
Run this task manually to immediately scan your product catalog for outdated variants, and delete them. This task will also run nightly, at midnight in your store's timezone.
Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.
That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.
(By the way, have you seen our documentation? Have you joined the Slack community?)
mechanic/user/trigger mechanic/scheduler/daily mechanic/shopify/bulk_operation
{% assign date_metafield_namespace = options.date_metafield_namespace__required %} {% assign date_metafield_key = options.date_metafield_key__required %} {% assign today = "now" | date: "%Y-%m-%d" %} {% if event.topic == "mechanic/user/trigger" or event.topic == "mechanic/scheduler/daily" %} {% comment %} -- get all active, published, multi-variant products in the shop {% endcomment %} {% capture bulk_operation_query %} query { products( query: "status:active published_status:published has_only_default_variant:false" ) { edges { node { __typename id variants { edges { node { __typename id metafield( namespace: {{ date_metafield_namespace | json }} key: {{ date_metafield_key | json }} ) { value } } } } } } } } {% endcapture %} {% action "shopify" %} mutation { bulkOperationRunQuery( query: {{ bulk_operation_query | json }} ) { bulkOperation { id status } userErrors { field message } } } {% endaction %} {% elsif event.topic == "mechanic/shopify/bulk_operation" %} {% if event.preview %} {% capture jsonl_string %} {"__typename":"Product","id":"gid://shopify/Product/1234567890"} {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/1234567890","metafield":{"value":"2020-02-02"},"__parentId":"gid://shopify/Product/1234567890"} {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/2345678901","__parentId":"gid://shopify/Product/1234567890"} {% endcapture %} {% assign bulkOperation = hash %} {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %} {% endif %} {% assign products = bulkOperation.objects | where: "__typename", "Product" %} {% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %} {% comment %} -- for each product, qualify and delete variants in bulk by metafield value {% endcomment %} {% for product in products %} {% assign variants = bulk_variants | where: "__parentId", product.id %} {% assign variant_ids_to_delete = array %} {% for variant in variants %} {% if variant.metafield != blank and variant.metafield.value < today %} {% assign variant_ids_to_delete = variant_ids_to_delete | push: variant.id %} {% endif %} {% endfor %} {% if variant_ids_to_delete != blank %} {% action "shopify" %} mutation { productVariantsBulkDelete( productId: {{ product.id | json }} variantsIds: {{ variant_ids_to_delete | json }} ) { product { id title } userErrors { code field message } } } {% endaction %} {% endif %} {% endfor %} {% endif %}