Mechanic Get Mechanic
Task library

Add all products to a certain sales channel

Does exactly as it says: this task will publish all active products to the sales channel(s) of your choice, on demand. Optionally, filter products by a search query, or choose to have this task run hourly or daily.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Sales channel names
  • Only publish products matching this query
  • Test mode
  • Run hourly
  • Run daily

Documentation

Does exactly as it says: this task will publish all active products to the sales channel(s) of your choice, on demand. Optionally, filter products by a search query, or choose to have this task run hourly or daily.

Note: this task comes with a "test mode", which is enabled by default. Run the task at least once in this mode to make sure it would publish the products you expect, and then disbale the test mode.

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

mechanic/user/trigger
mechanic/shopify/bulk_operation
{% if options.run_hourly__boolean %}
  mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign sales_channel_names = options.sales_channel_names__required_array %}
{% assign products_query = options.only_publish_products_matching_this_query %}
{% assign test_mode = options.test_mode__boolean %}

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

{% assign result = query | shopify %}

{% assign publications = array %}

{% for publication in result.data.publications.nodes %}
  {% if sales_channel_names contains publication.name %}
    {% assign publications[publications.size] = publication %}
  {% endif %}
{% endfor%}

{% if event.preview %}
  {% assign publications[0] = hash %}
  {% assign publications[0]["id"] = "gid://shopify/Publication/1234567890" %}
  {% assign publications[0]["name"] = sales_channel_names.first %}
{% endif %}

{% if publications.size != sales_channel_names.size %}
  {% unless event.preview %}
    {% log
      publications_named: sales_channel_names,
      publications_available: result.data.publications.nodes,
      publications_matched: publications
    %}
    {% error "Unable to find all configured publications. Double-check your task configuration." %}
  {% endunless %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% unless products_query contains "status:active" %}
    {% assign products_query = products_query | append: " status:active" | strip %}
  {% endunless %}

  {% capture bulk_operation_query %}
    query {
      products (
        query: {{ products_query | json }}
      ) {
        edges {
          node {
            __typename
            id
            {% for publication in publications %}
              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 %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture jsonl_string %}
      {"__typename":"Product","id":"gid://shopify/Product/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
  {% assign product_ids_and_publication_ids = array %}

  {% for product in products %}
    {% for publication in publications %}
      {% assign published_key = "publishedOnPublication" | append: forloop.index %}

      {% unless product[published_key] %}
        {% assign pair = array %}
        {% assign pair[0] = product.id %}
        {% assign pair[1] = publication.id %}
        {% assign product_ids_and_publication_ids[product_ids_and_publication_ids.size] = pair %}
      {% endunless %}
    {% endfor %}
  {% endfor %}

  {% if test_mode %}
    {% log
      message: "Test mode: publishing actions logged only.",
      publishing_actions_count: product_ids_and_publication_ids.size,
      product_ids_and_publication_ids: product_ids_and_publication_ids
    %}

  {% else %}
    {% for keyval in product_ids_and_publication_ids %}
      {% action "shopify" %}
        mutation {
          publishablePublish(
            id: {{ keyval[0] | json }}
            input: {
              publicationId: {{ keyval[1] | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic Liquid reference

Default option values

Sales channel names
["Online Store"]
Test mode
true