Mechanic Get Mechanic
Task library

Unpublish products when tagged

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

Try this task

When it runs

Occurs whenever a product is updated, ordered, or variants are added, removed or updated

What you choose

  • Product tags to watch for
  • Require product to have all given tags before unpublishing
  • Sales channel names
Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/products/update
How task subscriptions work

Option types and flags

Task code

{% 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 Liquid reference

Default option values

Sales channel names
["Online Store"]