Auto-tag products with incoming inventory
Keep track of which products in your shop have incoming inventory with this task! Running when inventory levels are updated, this task will tag the product with the tag of your choice if any of that product's variants at any of your locations has an incoming transfer. Conversely, the tag will be removed when there are no pending transfers.
Try this taskWhen it runs
- Occurs whenever an inventory level is updated
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- Product tag to apply
- Run on a schedule
Documentation
Keep track of which products in your shop have incoming inventory with this task! Running when inventory levels are updated, this task will tag the product with the tag of your choice if any of that product's variants at any of your locations has an incoming transfer. Conversely, the tag will be removed when there are no pending transfers.
The task may also be run manually, and optionally on a schedule, in which case it will scan your entire product catalog for incoming transfers, and tag products accordingly.
Notes:
- Transfers initiated from suppliers (i.e external locations) do not trigger inventory level updates until they are received. Use the "Run on a schedule" option to have the task identify and tag these incoming transfers.
- Because the scheduled run option uses a bulk operation query, by default it has been scheduled to run at 2 AM local shop time to avoid potential conflicts with other scheduled bulk operations. Adjust this time and frequency as needed in the task subscriptions.
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
shopify/inventory_levels/update
{% if options.run_on_a_schedule__boolean %}
mechanic/scheduler/daily+2.hours
{% endif %}
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Option types and flags
- Product tag to apply (required)
- Run on a schedule (boolean)
Task code
{% assign product_tag_to_apply = options.product_tag_to_apply__required | strip %}
{% if event.topic == "shopify/inventory_levels/update" %}
{% comment %}
-- get product ID and tags using the inventory_item_id in the inventory_level webhook
{% endcomment %}
{% capture query %}
query {
inventoryItem(
id: {{ inventory_level.inventory_item_id | prepend: "gid://shopify/InventoryItem/" | json }}
) {
variant {
product {
id
tags
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryItem": {
"variant": {
"product": {
"id": "gid://shopify/Product/1234567890"
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign product = result.data.inventoryItem.variant.product %}
{% comment %}
-- get all variants (up to 2K) for this product and their "incoming" inventory levels (up to 200 locations)
{% endcomment %}
{% assign qualifies_to_be_tagged = nil %}
{% assign cursor = nil %}
{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.id | json }}) {
variants(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
inventoryItem {
inventoryLevels(first: 200) {
nodes {
quantities(names: "incoming") {
name
quantity
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"id": "gid://shopify/Product/1234567890",
"variants": {
"nodes": [
{
"inventoryItem": {
"inventoryLevels": {
"nodes": [
{
"quantities": [
{
"name": "incoming",
"quantity": 1
}
]
}
]
}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% comment %}
-- the product qualifies if any of the variants' inventory levels have incoming inventory
{% endcomment %}
{% for variant in result.data.product.variants.nodes %}
{% for inventory_level in variant.inventoryItem.inventoryLevels.nodes %}
{% if inventory_level.quantities.first.quantity > 0 %}
{% assign qualifies_to_be_tagged = true %}
{% break %}
{% endif %}
{% endfor %}
{% if qualifies_to_be_tagged %}
{% break %}
{% endif %}
{% endfor %}
{% if qualifies_to_be_tagged or result.data.product.variants.pageInfo.hasNextPage == false %}
{% break %}
{% else %}
{% assign cursor = result.data.product.variants.pageInfo.endCursor %}
{% endif %}
{% endfor %}
{% if qualifies_to_be_tagged %}
{% unless product.tags contains product_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ product.id | json }}
tags: {{ product_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% else %}
{% if product.tags contains product_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ product.id | json }}
tags: {{ product_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- run bulk op query to get all active products in the shop, and their variants' "incoming" inventory levels
{% endcomment %}
{% capture bulk_operation_query %}
query {
products(
query: "status:active"
) {
edges {
node {
__typename
id
tags
variants {
edges {
node {
__typename
id
inventoryItem {
inventoryLevels {
edges {
node {
__typename
id
quantities(names: "incoming") {
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" %}
{% if event.preview %}
{% capture jsonl_string %}
{"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","tags":["testing"]}
{"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/1234567890","inventoryItem":{},"__parentId":"gid:\/\/shopify\/Product\/1234567890"}
{"__typename":"InventoryLevel","quantities":[{"name":"incoming","quantity":1}],"__parentId":"gid:\/\/shopify\/ProductVariant\/1234567890"}
{"__typename":"Product","id":"gid:\/\/shopify\/Product\/2345678901","tags":[{{ product_tag_to_apply | json }}]}
{"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/2345678901","inventoryItem":{},"__parentId":"gid:\/\/shopify\/Product\/2345678901"}
{"__typename":"InventoryLevel","quantities":[{"name":"incoming","quantity":1}],"__parentId":"gid:\/\/shopify\/ProductVariant\/2345678901"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = jsonl_string | 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 qualifies_to_be_tagged = nil %}
{% assign variants = bulk_variants | where: "__parentId", product.id %}
{% for variant in variants %}
{% assign inventory_levels = bulk_inventory_levels | where: "__parentId", variant.id %}
{% for inventory_level in inventory_levels %}
{% if inventory_level.quantities.first.quantity > 0 %}
{% assign qualifies_to_be_tagged = true %}
{% break %}
{% endif %}
{% endfor %}
{% if qualifies_to_be_tagged %}
{% break %}
{% endif %}
{% endfor %}
{% if qualifies_to_be_tagged %}
{% unless product.tags contains product_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ product.id | json }}
tags: {{ product_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% else %}
{% if product.tags contains product_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ product.id | json }}
tags: {{ product_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference