Add all products to a certain sales channel, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Add all products to a certain sales channel

Does exactly as it says: this task will publish all 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.

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes sales channel names, only publish products matching this query, test mode, run hourly, and run daily.

15-day free trial – unlimited tasks

Documentation

Does exactly as it says: this task will publish all 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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
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 %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
sales channel names (required, array), only publish products matching this query, test mode (boolean), run hourly (boolean), run daily (boolean)
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 %}
  {% 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." %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Sales channel names
["Online Store"]
Test mode
true