Mechanic is a development and ecommerce automation platform for Shopify. :)
This task scans your active products, in bulk, and tags them according to whether or not they're in stock. Optionally, configure this task to monitor products for live auto-tagging.
Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes in stock tag, out of stock tag, and monitor products for inventory updates.
This task scans your active products, in bulk, and tags them according to whether or not they're in stock. Optionally, configure this task to monitor products for live auto-tagging.
For the purposes of this task, "in stock" means "having a total inventory of greater than zero, when added up for all variants across all locations".
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/shopify/bulk_operation
{% if options.monitor_products_for_inventory_updates__boolean %}
shopify/products/create
shopify/products/update
{% endif %}{% assign in_stock_tag = options.in_stock_tag__required %}
{% assign out_of_stock_tag = options.out_of_stock_tag__required %}
{% assign products = array %}
{% if event.topic contains "shopify/products/" %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
id
status
totalInventory
tags
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if result.data.product.status == "ACTIVE" or event.preview %}
{% assign products[0] = result.data.product %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% capture bulk_operation_query %}
query {
products(
query: "status:active"
) {
edges {
node {
__typename
id
totalInventory
tags
}
}
}
}
{% 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 products = bulkOperation.objects | where: "__typename", "Product" %}
{% endif %}
{% if event.preview %}
{% capture products_json %}
[
{
"id": "gid://shopify/Product/1234567890",
"totalInventory": 1
},
{
"id": "gid://shopify/Product/2345678901",
"totalInventory": 0
}
]
{% endcapture %}
{% assign products = products_json | parse_json %}
{% endif %}
{% for product in products %}
{% assign tag_to_add = nil %}
{% assign tag_to_remove = nil %}
{% if product.totalInventory > 0 %}
{% unless product.tags contains in_stock_tag %}
{% assign tag_to_add = in_stock_tag %}
{% endunless %}
{% if product.tags contains out_of_stock_tag %}
{% assign tag_to_remove = out_of_stock_tag %}
{% endif %}
{% else %}
{% unless product.tags contains out_of_stock_tag %}
{% assign tag_to_add = out_of_stock_tag %}
{% endunless %}
{% if product.tags contains in_stock_tag %}
{% assign tag_to_remove = in_stock_tag %}
{% endif %}
{% endif %}
{% if tag_to_add != blank or tag_to_remove != blank %}
{% action "shopify" %}
mutation {
{% if tag_to_add != blank %}
tagsAdd(
id: {{ product.id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
{% endif %}
{% if tag_to_remove != blank %}
tagsRemove(
id: {{ product.id | json }}
tags: {{ tag_to_remove | json }}
) {
userErrors {
field
message
}
}
{% endif %}
}
{% endaction %}
{% endif %}
{% endfor %}
in-stock
out-of-stock