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

Mechanic is an automation development platform for Shopify. :)

Add all products to a certain sales channel

by Isaac Bowen (team@usemechanic.com)

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 every ten minutes, hourly, or daily.

Runs when a user triggers the task. Configuration includes sales channel names, only publish products matching this query, test mode, run every 10 minutes, run hourly, and run daily.

15-day free trial – unlimited tasks

Developer details

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

Events
when a user triggers the task (mechanic/user/trigger)
Options
sales channel names (required, array), only publish products matching this query, test mode (boolean), run every 10 minutes (boolean), run hourly (boolean), run daily (boolean)
Script
{% capture query %}
  query {
    publications(first: 250) {
      edges {
        node {
          id
          name
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% 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 %}

{% assign cursor = nil %}
{% assign product_ids_and_publication_ids = array %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        query: {{ options.only_publish_products_matching_this_query | json }}
        after: {{ cursor | json }}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            {% for publication in publications %}
              publishedOnPublication{{ forloop.index }}: publishedOnPublication(
                publicationId: {{ publication.id | json }}
              )
            {% endfor %}
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% for product_edge in result.data.products.edges %}
    {% assign product = product_edge.node %}
    {% 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 result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% if event.preview %}
  {% assign product_ids_and_publication_ids[0] = array %}
  {% assign product_ids_and_publication_ids[0] = "gid://shopify/Product/1234567890" %}
  {% assign product_ids_and_publication_ids[0] = "gid://shopify/Publication/1234567890" %}
{% endif %}

{% if options.test_mode__boolean %}
  {% action "echo" %}
    {
      "message": "Found {{ product_ids_and_publication_ids.size }} publishing actions",
      "product_ids_and_publication_ids": {{ product_ids_and_publication_ids | json }}
    }
  {% endaction %}
{% else %}
  {% for product_id_and_publication_id in product_ids_and_publication_ids %}
    {% action "shopify" %}
      mutation {
        publishablePublish(
          id: {{ product_id_and_publication_id[0] | json }}
          input: {
            publicationId: {{ product_id_and_publication_id[1] | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Sales channel names
["Online Store"]
Test mode
true