Auto-tag products based on their publishing status, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag products based on their publishing status

by Isaac Bowen (team@usemechanic.com)

This task manages tagging for products, according to whether or not they're published for the sales channel of your choice. Choose a tag to use when the product is published, and/or a tag for when the product is unpublished.

Runs when a user triggers the task. Configuration includes tag to add when published, tag to add when unpublished, sales channel name, run every 10 minutes, run hourly, and run daily.

15-day free trial – unlimited tasks

Documentation

This task manages tagging for products, according to whether or not they're published for the sales channel of your choice. Choose a tag to use when the product is published, and/or a tag for when the product is unpublished.

Run this task manually to scan and update your entire product catalog, on demand. Otherwise, configure this task to perform this same scan on a schedule. (Note: It's not possible for a task to respond instantly to publishing or unpublishing a product. Instead, configure a run schedule.)

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)
Options
tag to add when published, tag to add when unpublished, sales channel name (required), run every 10 minutes (boolean), run hourly (boolean), run daily (boolean)
Script
{% if options.tag_to_add_when_published == blank and options.tag_to_add_when_unpublished == blank %}
  {% error "Fill in at least one tag option to continue. :)" %}
{% endif %}

{% capture query %}
  query {
    publications(first: 250) {
      edges {
        node {
          id
          name
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% assign publications = result.data.publications.edges | map: "node" | where: "name", options.sales_channel_name__required | first %}
{% assign publication = publications | where: "name", options.sales_channel_name__required | first %}

{% assign cursor = nil %}
{% assign mutations = array %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        after: {{ cursor | json }}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            tags
            published: publishedOnPublication(
              publicationId: {{ publication.id | json }}
            )
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "products": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Product/1234567890",
                  "tags": [],
                  "published": {% if options.tag_to_add_when_published != blank %}true{% else %}false{% endif %}
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% for product_edge in result.data.products.edges %}
    {% assign product = product_edge.node %}
    {% assign tag_to_add = nil %}
    {% assign tag_to_remove = nil %}

    {% if options.tag_to_add_when_published != blank %}
      {% if product.published %}
        {% unless product.tags contains options.tag_to_add_when_published %}
          {% assign tag_to_add = options.tag_to_add_when_published %}
        {% endunless %}
      {% elsif product.tags contains options.tag_to_add_when_published %}
        {% assign tag_to_remove = options.tag_to_add_when_published %}
      {% endif %}
    {% endif %}

    {% if options.tag_to_add_when_unpublished != blank %}
      {% if product.published == false %}
        {% unless product.tags contains options.tag_to_add_when_unpublished %}
          {% assign tag_to_add = options.tag_to_add_when_unpublished %}
        {% endunless %}
      {% elsif product.tags contains options.tag_to_add_when_unpublished %}
        {% assign tag_to_remove = options.tag_to_add_when_unpublished %}
      {% endif %}
    {% endif %}

    {% if tag_to_add or tag_to_remove %}
      {% log product: product %}

      {% action "shopify" %}
        mutation {
          {% if tag_to_add %}
            tagsAdd(
              id: {{ product.id | json }}
              tags: {{ tag_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}

          {% if tag_to_remove %}
            tagsRemove(
              id: {{ product.id | json }}
              tags: {{ tag_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}

  {% if result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.