Mechanic Get Mechanic
Task library

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 or purchase order. Conversely, the tag will be removed when there are no in transit transfers or ordered purchase orders.

Try this task

When 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 or purchase order. Conversely, the tag will be removed when there are no in transit transfers or ordered purchase orders.

The task may also be run manually, and optionally on a schedule, in which case it will scan your entire product catalog for incoming inventory, and tag products accordingly.

Notes:
- Transfers initiated without an origin, and all purchase orders, do not trigger inventory level updates until they are received. Use the "Run on a schedule" option to have the task run daily to also identify these incoming inventory while they are in transit.

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

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
  {% endcomment %}

  {% capture query %}
    query {
      inventoryItem(
        id: {{ inventory_level.inventory_item_id | prepend: "gid://shopify/InventoryItem/" | json }}
      ) {
        variants(first: 1) {
          nodes {
            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.variants.nodes.first.product %}

  {% comment %}
    -- get all variants for this product and their "incoming" inventory levels (up to 200 locations)
  {% endcomment %}

  {% assign qualifies_to_be_tagged = nil %}

  {% capture query %}
    query {
      product(id: {{ product.id | json }}) {
        variants(
          first: 2048
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            inventoryItem {
              inventoryLevels(first: 200) {
                nodes {
                  quantities(names: "incoming") {
                    quantity
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "product": {
            "variants": {
              "nodes": [
                {
                  "inventoryItem": {
                    "inventoryLevels": {
                      "nodes": [
                        {
                          "quantities": [
                            {
                              "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 %}
    {% 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") {
                            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":[{"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":[{"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