Mechanic Get Mechanic
Task library

Auto publish products by tag

This task runs when products are created or updated 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 created
  • Occurs whenever a product is updated, ordered, or variants are added, removed or updated
  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Sales channel names
  • Product tag

Documentation

This task runs when products are created or updated 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/create
shopify/products/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
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 %}

{% comment %}
  -- get all of the sales channel names (i.e. publications aka app catalogs) in the shop
{% 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_names.first | json }}
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% comment %}
  -- save the publication IDs for all of the configured sales channel names
{% endcomment %}

{% assign publication_ids = array %}
{% 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_names contains publication_name %}
    {% assign publication_ids = publication_ids | push: publication.id %}
  {% endif %}
{% endfor %}

{% if publication_ids == blank  %}
  {% error
    message: "None of the sales channel configured in this task exist in the shop. Check the list of available channels and verify each configured channel exists.",
    configured_sales_channel_names: sales_channel_names,
    available_sales_channel_names: available_sales_channel_names
  %}

  {% break %}

{% elsif publication_ids.size != sales_channel_names.size %}
  {% unless event.preview %}
    {% comment %}
      -- using action error here so the task will continue with any other configured and matched sales channels
    {% endcomment %}

    {% action "echo"
      __error: "One or more configured sales channel names do not match any of the publication names available in this shop.",
      configured_sales_channel_names: sales_channel_names,
      available_sales_channel_names: available_sales_channel_names
    %}
  {% endunless %}
{% endif %}

{% if event.topic contains "shopify/products/" %}
  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        id
        tags
        {% for publication_id in publication_ids %}
          publishedOnPublication{{ forloop.index }}: publishedOnPublication(
            publicationId: {{ publication_id | json }}
          )
        {% endfor %}
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign products = array | push: result.data.product %}

{% elsif event.topic contains "mechanic/user/trigger" %}
  {% comment %}
    -- get all active products in the shop
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      products (
        query: "status:active"
      ) {
        edges {
          node {
            __typename
            id
            tags
            {% for publication_id in publication_ids %}
              publishedOnPublication{{ forloop.index }}: publishedOnPublication(
                publicationId: {{ publication_id | json }}
              )
            {% endfor %}
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

  {% break %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
{% endif %}

{% if event.preview %}
  {% capture products_json %}
    [
      {
        "id": "gid://shopify/Product/1234567890",
        "tags": {{ array | push: product_tag | json }}
      }
    ]
  {% endcapture %}

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

{% comment %}
  -- process all products returned by bulk op query or the product that triggered the event
{% endcomment %}

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

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

  {% for publication_id in publication_ids %}
    {% assign published_key = "publishedOnPublication" | append: forloop.index %}

    {% if product[published_key] %}
      {% unless should_be_published %}
        {% capture mutation %}
          publishableUnpublish{{ publication_id | remove: "gid://shopify/Publication/" }}: publishableUnpublish(
            id: {{ product.id | json }}
            input: {
              publicationId: {{ publication_id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        {% endcapture %}

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

    {% elsif should_be_published %}
      {% capture mutation %}
        publishablePublish{{ publication_id | remove: "gid://shopify/Publication/" }}: publishablePublish(
          id: {{ product.id | json }}
          input: {
            publicationId: {{ 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
[]