Auto-tag products that are missing costs with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Auto-tag products that are missing costs

by Isaac Bowen (team@usemechanic.com)

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 when a user triggers the task, when a product is created, and when a product is updated. Configuration includes tag for cost missing.

15-day free trial – unlimited tasks

Documentation

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, Gurus, 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.

Events
when a user triggers the task (mechanic/user/trigger)
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
tag for cost missing (required)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          12345
        ],
        {
          "tags": {{ options.tag_for_cost_missing__required | json }}
        }
      ]
    }
  }
{% 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 tags_to_save = product.tags %}
    {% if missing_cost %}
      {% assign tags_to_save = tags_to_save | add_tag: options.tag_for_cost_missing__required %}
    {% else %}
      {% assign tags_to_save = tags_to_save | remove_tag: options.tag_for_cost_missing__required %}
    {% endif %}

    {% if tags_to_save != product.tags %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "product",
              {{ product.id | json }}
            ],
            {
              "tags": {{ tags_to_save | json }}
            }
          ]
        }
      }
    {% 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 tags_to_save = product.tags %}
  {% if missing_cost %}
    {% assign tags_to_save = tags_to_save | add_tag: options.tag_for_cost_missing__required %}
  {% else %}
    {% assign tags_to_save = tags_to_save | remove_tag: options.tag_for_cost_missing__required %}
  {% endif %}

  {% if tags_to_save != product.tags %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "product",
            {{ product.id | json }}
          ],
          {
            "tags": {{ tags_to_save | json }}
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.