Sync an inverse product tag, with Mechanic.

Mechanic is a development platform for Shopify. :)

Sync an inverse product tag

Shopify's product collection filters allow you to filter by the tags the product contains – but not the tags a product does not contain. Use this task to sync an "inverse" tag to all products who do not have a particular tag, allowing you to create collections in Shopify that use the inverse tag.

Runs when a product is created, when a product is updated, when a user triggers the task, and when a bulk operation finishes. Configuration includes product tag to watch and product tag to use when missing.

15-day free trial – unlimited tasks

Documentation

Shopify's product collection filters allow you to filter by the tags the product contains – but not the tags a product does not contain. Use this task to sync an "inverse" tag to all products who do not have a particular tag, allowing you to create collections in Shopify that use the inverse tag.

To explain by example, if you've already tagged your wholesale products with "wholesale", configure the "Product tag to watch" option with "wholesale", and the "Product tag to use when missing" option with "retail". This task will automatically tag all non-wholesale products with "retail", and automatically remove that tag from any products who later receive the tag "wholesale". Please note: if you remove the "retail" tag, this task will simply re-add it. To remove the "retail" tag, you would need to add the "wholesale" tag to that product.

Run this task manually to scan all of the products in your store. Otherwise, this task will monitor and sync tags for all new and updated products, as they come through.

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.

Open source
View on GitHub to contribute to this task
Events
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
product tag to watch (required), product tag to use when missing (required)
Script
{% assign products = array %}

{% if event.topic contains "shopify/products/" %}
  {% if event.preview %}
    {% assign product = hash %}
    {% assign product["admin_graphql_api_id"] = "gid://shopify/Product/1234567890" %}
    {% assign product["tags"] = "" %}
  {% endif %}

  {% assign product_node = hash %}
  {% assign product_node["id"] = product.admin_graphql_api_id %}
  {% assign product_node["tags"] = product.tags | split: ", " %}
  {% assign products[0] = product_node %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            id
            tags
          }
        }
      }
    }
  {% 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 objects_jsonl %}
      {"id":"gid://shopify/Product/1234567890","tags": []}
    {% endcapture %}

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

  {% assign products = bulkOperation.objects %}
{% endif %}

{% for product in products %}
  {% assign add_tag = false %}
  {% assign remove_tag = false %}

  {% if product.tags contains options.product_tag_to_watch__required %}
    {% if product.tags contains options.product_tag_to_use_when_missing__required %}
      {% assign remove_tag = true %}
    {% endif %}
  {% else %}
    {% unless product.tags contains options.product_tag_to_use_when_missing__required %}
      {% assign add_tag = true %}
    {% endunless %}
  {% endif %}

  {% if add_tag or remove_tag %}
    {% action "shopify" %}
      mutation {
        tags{% if add_tag %}Add{% else %}Remove{% endif %}(
          id: {{ product.id | json }}
          tags: {{ options.product_tag_to_use_when_missing__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Product tag to watch
wholesale
Product tag to use when missing
retail