Mechanic Get Mechanic
Task library

Auto publish products by tag

This task runs when products are updated (which includes product creation) and manages publishing to the configured sales channels, according to whether or not the product has a certain tag. Products with the tag will be published on ALL of the configured sales channels; and those without the product tag will be unpublished from them.

Try this task

When it runs

  • Occurs whenever a product is updated, ordered, or variants are added, removed or updated
  • Occurs when a user manually triggers the task

What you choose

  • Sales channel names
  • Product tag

Documentation

This task runs when products are updated (which includes product creation) and manages publishing to the configured sales channels, according to whether or not the product has a certain tag. Products with the tag will be published on ALL of the configured sales channels; and those without the product tag will be unpublished from them.

Run this task manually to scan your entire product catalog, publishing and unpublishing products by the presence or absence of the configured product tag.

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
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign sales_channel_names = options.sales_channel_names__array_required %}
{% assign product_tag = options.product_tag__required %}

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

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "publications": {
          "edges": [
            {% for sales_channel_name in sales_channel_names %}
              {
                "node": {
                  "id": "gid://shopify/Publication/{{ forloop.index }}",
                  "name": {{ sales_channel_name | json }}
                }
              }
              {% unless forloop.last %},{% endunless %}
            {% endfor %}
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign publications = result.data.publications.edges | map: "node" %}
{% assign publication_names = publications | map: "name" | sort %}

{% assign configured_publications = array %}

{% for publication in publications %}
  {% if sales_channel_names contains publication.name %}
    {% assign configured_publications = configured_publications | push: publication %}
  {% endif %}
{% endfor %}

{% log
  available_publications: publication_names,
  configured_publications: configured_publications
%}

{% capture published_on_publications_selections %}
  {% for configured_publication in configured_publications %}
    publishedOnPublication{{ configured_publication.id | split: "/" | last }}: publishedOnPublication(
      publicationId: {{ configured_publication.id | json }}
    )
  {%- endfor %}
{% endcapture %}

{% assign products = array %}

{% if event.topic contains "shopify/products/" %}
  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        id
        tags
        {{ published_on_publications_selections }}
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign products[0] = result.data.product %}

{% elsif event.topic contains "mechanic/user/trigger" %}
  {% assign cursor = nil %}

  {% for n in (0..300) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            node {
              id
              tags
              {{ published_on_publications_selections }}
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% assign products_batch = result.data.products.edges | map: "node" %}
    {% assign products = products | concat: products_batch %}

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

{% if event.preview %}
  {% capture products_json %}
    [
      {
        "id": "gid://shopify/Product/1234567890",
        {% for configured_publication in configured_publications %}
          "publishedOnPublication{{ configured_publication.id | split: "/" | last }}": false,
        {% endfor %}
        "tags": {{ product_tag | json }}
      }
    ]
  {% endcapture %}

  {% assign products = products_json | parse_json %}
{% endif %}

{% log products: products %}

{% for product in products %}
  {% assign mutations = array %}
  {% assign should_be_published = false %}

  {% if product.tags contains product_tag %}
    {% assign should_be_published = true %}
  {% endif %}

  {% for configured_publication in configured_publications %}
    {% assign publication_key = configured_publication.id | split: "/" | last | prepend: "publishedOnPublication" %}

    {% if product[publication_key] %}
      {% unless should_be_published %}
        {% capture mutation %}
          publishableUnpublish{{ configured_publication.id | split: "/" | last }}: publishableUnpublish(
            id: {{ product.id | json }}
            input: {
              publicationId: {{ configured_publication.id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        {% endcapture %}

        {% assign mutations = mutations | push: mutation %}
      {% endunless %}

    {% elsif should_be_published %}
      {% capture mutation %}
        publishablePublish{{ configured_publication.id | split: "/" | last }}: publishablePublish(
          id: {{ product.id | json }}
          input: {
            publicationId: {{ configured_publication.id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      {% endcapture %}

      {% assign mutations = mutations | push: mutation %}
    {% endif %}
  {% endfor %}

  {% if mutations != blank %}
    {% action "shopify" %}
      mutation {
        {{ mutations | join: newline }}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference

Default option values

Sales channel names
[]