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

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-tag products based on their publishing status

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 Occurs when a user manually 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, 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

{% if options.run_every_10_minutes__boolean %}
  mechanic/scheduler/10min
{% elsif options.run_hourly__boolean %}
  mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more