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 when a user manually triggers the task, Occurs whenever a product is created, and Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated. 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
mechanic/user/trigger
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag for cost missing (required)
Code
{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Product/1234567890"
        tags: {{ options.tag_for_cost_missing__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for product in shop.products %}
    {% assign missing_cost = true %}
    {% for variant in product.variants %}
      {% if variant.inventory_item.cost != blank %}
        {% assign missing_cost = false %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% assign product_tags = product.tags | split: ", " %}

    {% if missing_cost %}
      {% unless product_tags contains options.tag_for_cost_missing__required %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ product.admin_graphql_api_id | json }}
              tags: {{ options.tag_for_cost_missing__required | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}
    {% elsif product_tags contains options.tag_for_cost_missing__required %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ options.tag_for_cost_missing__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% elsif event.topic == "shopify/products/create" or event.topic == "shopify/products/update" %}
  {% assign missing_cost = true %}
  {% for variant in product.variants %}
    {% if variant.inventory_item.cost != blank %}
      {% assign missing_cost = false %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% assign product_tags = product.tags | split: ", " %}

  {% if missing_cost %}
    {% unless product_tags contains options.tag_for_cost_missing__required %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ options.tag_for_cost_missing__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% elsif product_tags contains options.tag_for_cost_missing__required %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ product.admin_graphql_api_id | json }}
          tags: {{ options.tag_for_cost_missing__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more