Tag products with no images, with Mechanic.

Mechanic is a development platform for Shopify. :)

Tag products with no images

by Isaac Bowen (team@usemechanic.com)

Running manually or on a schedule, this task scans all products in your store, and adds a tag to any products that have no images. Optionally, choose to have this task remove that tag from any products that are found to have an image.

Runs when a user triggers the task and when a bulk operation finishes. Configuration includes product tag to add, remove product tag if images are found, run every 10 minutes, run hourly, and run daily.

15-day free trial – unlimited tasks

Documentation

Running manually or on a schedule, this task scans all products in your store, and adds a tag to any products that have no images. Optionally, choose to have this task remove that tag from any products that are found to have an image.

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 bulk operation finishes (mechanic/shopify/bulk_operation)
Options
product tag to add (required), remove product tag if images are found (boolean), run every 10 minutes, run hourly (boolean), run daily (boolean)
Script
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            __typename
            id
            tags
            images {
              edges {
                node {
                  __typename
                  id
                }
              }
            }
          }
        }
      }
    }
  {% 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 bulkOperation_objects_jsonl %}
      {"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","tags":[]}
    {% endcapture %}

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

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
  {% assign images = bulkOperation.objects | where: "__typename", "Image" %}
  
  {% for product in products %}
    {% assign product_images = images | where: "__parentId", product.id %}
    {% if product_images.size == 0 %}
      {% unless product.tags contains options.product_tag_to_add__required %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ product.id | json }}
              tags: {{ options.product_tag_to_add__required | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endunless %}
    {% else %}
      {% if product.tags contains options.product_tag_to_add__required and options.remove_product_tag_if_images_are_found__boolean %}
        {% action "shopify" %}
          mutation {
            tagsRemove(
              id: {{ product.id | json }}
              tags: {{ options.product_tag_to_add__required | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.