Mechanic Get Mechanic
Task library

Auto-tag products that are missing costs

Use this task to help you close in on the last few products that are missing cost values. Trigger this task manually to process your entire catalog, or let this task update tags as needed for products you create or update.

Try this task

When it runs

  • Occurs whenever a product is created
  • Occurs whenever a product is updated, ordered, or variants are added, removed or updated
  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

Tag for cost missing

Documentation

Use this task to help you close in on the last few products that are missing cost values. Trigger this task manually to process your entire catalog, or let this task update tags as needed for products you create or update.

Use the "Run task" button to scan all products in your catalog. Or, create/update products as usual, and this task will maintain tagging accordingly.

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/products/create
shopify/products/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task code

{% assign tag_for_cost_missing = options.tag_for_cost_missing__required %}

{% if event.topic == "shopify/products/create" or event.topic == "shopify/products/update" %}
  {% assign missing_cost = nil %}
  {% assign cursor = nil %}

  {% comment %}
    -- query costs of variant inventory items, breaking if one is found without a cost; support up to 2k variants
  {% endcomment %}

  {% for n in (1..8) %}
    {% capture query %}
      query {
        product(id: {{ product.admin_graphql_api_id | json }}) {
          id
          tags
          variants(
            first: 250
            after: {{ cursor | json }}
          ) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              inventoryItem {
                unitCost {
                  amount
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "product": {
              "id": "gid://shopify/Product/1234567890",
              "variants": {
                "nodes": [
                  {
                    "inventoryItem": {
                      "unitCost": null
                    }
                  }
                ]
              }

            }
          }
        }
      {% endcapture %}

      {% assign result = result_json | parse_json %}
    {% endif %}

    {% assign product = result.data.product %}

    {% for variant in product.variants.nodes %}
      {% if variant.inventoryItem.unitCost == blank %}
        {% assign missing_cost = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if missing_cost %}
      {% break %}
    {% endif %}

    {% if result.data.product.variants.pageInfo.hasNextPage %}
      {% assign cursor = result.data.product.variants.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if missing_cost %}
    {% unless product.tags contains tag_for_cost_missing %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ tag_for_cost_missing | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}

  {% elsif product.tags contains tag_for_cost_missing %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ product.id | json }}
          tags: {{ tag_for_cost_missing | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- query all products in the shop and their variant inventory items
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            __typename
            id
            tags
            variants {
              edges {
                node {
                  __typename
                  id
                  inventoryItem {
                    unitCost {
                      amount
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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"}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/1234567890","inventoryItem":{"unitCost":null},"__parentId":"gid://shopify/Product/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
  {% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}

  {% comment %}
    -- process all products returned by the bulk op query
  {% endcomment %}

  {% for product in products %}
    {% comment %}
      -- link the variants to the product and check if any have a missing unitCost
    {% endcomment %}

    {% assign variants = bulk_variants | where: "__parentId", product.id %}

    {% assign missing_cost = nil %}

    {% for variant in variants %}
      {% if variant.inventoryItem.unitCost == blank %}
        {% assign missing_cost = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if missing_cost %}
      {% unless product.tags contains tag_for_cost_missing %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ product.id | json }}
              tags: {{ tag_for_cost_missing | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}

    {% elsif product.tags contains tag_for_cost_missing %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tag_for_cost_missing | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference