Unpublish products when tagged, with Mechanic.

Mechanic is a development platform for Shopify. :)

Unpublish products when tagged

by Isaac Bowen (team@usemechanic.com)

This task watches for certain product tags, and removes the product from the selected sales channel(s) when those tags are found.

Runs when a product is updated. Configuration includes product tags to watch for, require product to have all given tags before unpublishing, and sales channel names.

15-day free trial – unlimited tasks

Documentation

This task watches for certain product tags, and removes the product from the selected sales channel(s) when those tags are found.

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 product is updated (shopify/products/update)
Options
product tags to watch for (required, array), require product to have all given tags before unpublishing (boolean), sales channel names (required, array)
Script
{% if event.preview %}
  {% assign product = hash %}
  {% assign product["admin_graphql_api_id"] = "gid://shopify/Product/1234567890" %}
  {% assign product["tags"] = options.product_tags_to_watch_for__required_array | join: ", " %}
{% endif %}

{% assign product_qualifies = false %}
{% assign product_tags = product.tags | split: ", " %}

{% if options.require_product_to_have_all_given_tags_before_unpublishing__boolean %}
  {% for tag in options.product_tags_to_watch_for__required_array %}
    {% if product_tags contains tag %}
      {% if forloop.last %}
        {% assign product_qualifies = true %}
      {% endif %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% else %}
  {% for tag in options.product_tags_to_watch_for__required_array %}
    {% if product_tags contains tag %}
      {% assign product_qualifies = true %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

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

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "publications": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Publication/1234567890",
                  "name": {{ options.sales_channel_names__required_array.first | json }}
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

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

  {% assign publications = array %}

  {% for publication_edge in result.data.publications.edges %}
    {% if options.sales_channel_names__required_array contains publication_edge.node.name %}
      {% assign publications[publications.size] = publication_edge.node %}
    {% endif %}
  {% endfor%}

  {% if event.preview != true and publications.size != options.sales_channel_names__required_array.size %}
    {% log publications_named: options.sales_channel_names__required_array, publications_available: result.data.publications.edges,  publications_matched: publications %}
    {% error "Unable to find all named publications. Double-check your task configuration." %}
  {% endif %}

  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        tags
        {% for publication in publications %}
          publishedOnPublication{{ forloop.index0 }}: publishedOnPublication(publicationId: {{ publication.id | json }})
        {% endfor %}
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "product": {
            "id": "gid://shopify/Product/1234567809",
            "publishedOnPublication0": true
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}

  {% endif %}

  {% assign publication_ids_to_unpublish_from = array %}

  {% for publication in publications %}
    {% assign key = "publishedOnPublication" | append: forloop.index0 %}

    {% if result.data.product[key] == false %}
      {% continue %}
    {% endif %}

    {% assign publication_ids_to_unpublish_from[publication_ids_to_unpublish_from.size] = publication.id %}
  {% endfor %}

  {% if publication_ids_to_unpublish_from != empty %}
    {% action "shopify" %}
      mutation {
        {% for publication_id in publication_ids_to_unpublish_from %}
          publishableUnpublish{{ forloop.index0 }}: publishableUnpublish(
            id: {{ product.admin_graphql_api_id | json }}
            input: {
              publicationId: {{ publication_id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        {% endfor %}
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Sales channel names
["Online Store"]