Tag products as in- or out-of-stock
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.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- In stock tag
- Out of stock tag
- Monitor products for inventory updates
Documentation
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".
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
mechanic/user/trigger
mechanic/shopify/bulk_operation
{% if options.monitor_products_for_inventory_updates__boolean %}
shopify/products/create
shopify/products/update
{% endif %}
How task subscriptions work
Option types and flags
- In stock tag (required)
- Out of stock tag (required)
- Monitor products for inventory updates (boolean)
Task code
{% 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 %}
Mechanic Liquid reference
Default option values
- In stock tag
in-stock- Out of stock tag
out-of-stock- Monitor products for inventory updates
false