Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes datetime product metafield namespace dot key, run hourly, and run daily.
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
Configure the "Datetime product metafield namespace dot key" field with the namespace and key (e.g. "custom.unpublish_after") for the product metafield that holds a Shopify date or date_time value. More information on setting up new Shopify metafields can be found here.
When you run this task (or as it runs hourly/daily, per your configuration), the task will look for products who have a metafield date/time value that's in the past. For qualifying products, all inventory items with a level greater than 0 will have their levels set to exactly 0, and all variants will have their inventory policies set to "deny" (preventing sales, once the variants are all out of stock).
Note: If you choose to use the date only type for the metafield, then the task will receive those dates as midnight local shop time. So, if this task encounters a metafield date that is the same as the task run date, then that metafield date will be in the past based on a seconds-since-midnight calculation.
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 {% if options.run_hourly__boolean %} mechanic/scheduler/hourly {% elsif options.run_daily__boolean %} mechanic/scheduler/daily {% endif %} mechanic/shopify/bulk_operation
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} {% capture bulk_operation_query %} query { products { edges { node { __typename id metafield(key: {{ options.datetime_product_metafield_namespace_dot_key__required | json }}) { value } variants { edges { node { __typename id inventoryPolicy inventoryItem { id inventoryLevels { edges { node { __typename id location { id } quantities(names: "available") { name quantity } } } } } } } } } } } } {% endcapture %} {% action "shopify" %} mutation { bulkOperationRunQuery( query: {{ bulk_operation_query | json }} ) { bulkOperation { id status } userErrors { field message } } } {% endaction %} {% elsif event.topic == "mechanic/shopify/bulk_operation" %} {% assign now_s = "now" | date: "%s" | times: 1 %} {% if event.preview %} {% capture bulkOperation_objects_jsonl %} {"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","metafield":{"value":"2023-04-01"}} {"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/1234567890","inventoryPolicy": "CONTINUE","inventoryItem":{"id":"gid:\/\/shopify\/InventoryItem\/1234567890"},"__parentId":"gid:\/\/shopify\/Product\/1234567890"} {"__typename":"InventoryLevel","id": "gid:\/\/shopify\/InventoryLevel\/1234567890?inventory_item_id=1234567890","location":{"id":"gid:\/\/shopify\/Location\/1234567890"},"quantities":[{"name":"available","quantity":4}],"__parentId":"gid:\/\/shopify\/ProductVariant\/1234567890"} {% endcapture %} {% assign bulkOperation = hash %} {% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %} {% endif %} {% assign bulk_products = bulkOperation.objects | where: "__typename", "Product" %} {% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %} {% assign bulk_inventory_levels = bulkOperation.objects | where: "__typename", "InventoryLevel" %} {% for product in bulk_products %} {% assign summary = array %} {% if product.metafield.value == blank %} {% continue %} {% endif %} {% assign product_date = product.metafield.value | parse_date: "%Y-%m-%d" %} {% if product_date == nil %} {% log message: "Found a product metafield value that doesn't match the Shopify date or date_time metafield format. Skipping.", product_metafield_value: product.metafield.value %} {% continue %} {% endif %} {% assign product_date_s = product.metafield.value | date: "%s" | times: 1 %} {% if product_date_s > now_s %} {% continue %} {% endif %} {% assign variants = bulk_variants | where: "__parentId", product.id %} {% assign variant_ids_to_update = array %} {% assign inventory_adjustments = array %} {% for variant in variants %} {% assign inventory_levels = bulk_inventory_levels | where: "__parentId", variant.id %} {% if variant.inventoryPolicy != "DENY" %} {% assign variant_ids_to_update = variant_ids_to_update | push: variant.id %} {% assign summary[summary.size] = "Change variant inventory policy for " | append: variant.id | append: " to DENY" %} {% endif %} {% for inventory_level in inventory_levels %} {% if inventory_level.quantities.first.quantity > 0 %} {% assign inventory_adjustment = hash %} {% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.id %} {% assign inventory_adjustment["locationId"] = inventory_level.location.id %} {% assign inventory_adjustment["delta"] = inventory_level.quantities.first.quantity | times: -1 %} {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %} {% assign summary[summary.size] = "Set available inventory for " | append: variant.id | append: " at location " | append: inventory_level.location.id | append: " to 0, by adjusting it by -" | append: inventory_level.quantities.first.quantity %} {% endif %} {% endfor %} {% endfor %} {% if summary != blank %} {% log product_id: product.id, updates_to_be_made: summary %} {% endif %} {% if inventory_adjustments != blank %} {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %} {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %} {% action "shopify" %} mutation { inventoryAdjustQuantities( input: { reason: "correction" name: "available" changes: {{ group_of_inventory_adjustments | graphql_arguments }} } ) { inventoryAdjustmentGroup { reason changes { name delta quantityAfterChange item { id sku } location { name } } } userErrors { code field message } } } {% endaction %} {% endfor %} {% endif %} {% if variant_ids_to_update != blank %} {% action "shopify" %} mutation { productVariantsBulkUpdate( allowPartialUpdates: true productId: {{ product.id | json }} variants: [ {% for variant_id in variant_ids_to_update -%} { id: {{ variant_id | json }} inventoryPolicy: DENY } {%- endfor %} ] ) { product { id title } productVariants { displayName sku inventoryPolicy } userErrors { field message } } } {% endaction %} {% endif %} {% endfor %} {% endif %}