Auto-tag products that are missing costs, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

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, and Occurs when a bulk operation is completed. Configuration includes tag for cost missing.

15-day free trial – unlimited tasks

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.

Developer details

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?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/products/create
shopify/products/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag for cost missing (required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more