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
{% assign tag_to_add_when_published = options.tag_to_add_when_published %}
{% assign tag_to_add_when_unpublished = options.tag_to_add_when_unpublished %}
{% assign sales_channel_name = options.sales_channel_name__required %}

{% if tag_to_add_when_published == blank and tag_to_add_when_unpublished == blank %}
  {% error "Fill in at least one tag option to continue. :)" %}
{% endif %}

{% comment %}
  -- get the ID for the configured sales channel
  -- Note: title filter does work for app catalog titles, so have to get all publications
{% endcomment %}

{% capture query %}
  query {
    publications(
      first: 250
      catalogType:APP
    ) {
      nodes {
        id
        catalog {
          ... on AppCatalog {
            apps(first: 1) {
              nodes {
                title
              }
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "publications": {
          "nodes": [
            {
              "id": "gid://shopify/Publication/1234567890",
              "catalog": {
                "apps": {
                  "nodes": [
                    {
                      "title": {{ sales_channel_name | json }}
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% comment %}
  -- make sure the configured sales channel name matches a publication in this shop
{% endcomment %}

{% assign publication_id = nil %}
{% assign available_sales_channel_names = array %}

{% for publication in result.data.publications.nodes %}
  {% assign publication_name = publication.catalog.apps.nodes.first.title %}

  {% assign available_sales_channel_names = available_sales_channel_names | push: publication_name %}

  {% if sales_channel_name == publication_name %}
    {% assign publication_id = publication.id %}
  {% endif %}
{% endfor %}

{% if publication_id == blank %}
  {% error
    message: "The sales channel configured in this task must exist in the shop. Check the list of available channels.",
    configured_sales_channel_name: sales_channel_name,
    available_sales_channel_names: available_sales_channel_names
  %}
  {% break %}
{% endif %}

{% comment %}
  -- get all active products in the shop (up to 25K)
{% endcomment %}

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

{% for n in (1..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        after: {{ cursor | json }}
        query: "status:active"
      ) {
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          id
          tags
          published: publishedOnPublication(
            publicationId: {{ publication_id | json }}
          )
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

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

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

  {% assign products = products | concat: result.data.products.nodes %}

  {% if result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.pageInfo.endCursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% comment %}
  -- check and tag/untag products as needed
{% endcomment %}

{% for product in products %}
  {% assign tag_to_add = nil %}
  {% assign tag_to_remove = nil %}

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

    {% elsif product.tags contains tag_to_add_when_published %}
      {% assign tag_to_remove = tag_to_add_when_published %}
    {% endif %}
  {% endif %}

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

    {% elsif product.tags contains tag_to_add_when_unpublished %}
      {% assign tag_to_remove = 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more