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: name your sales channel, and this task will publish all products to the sales channel of your choice, on demand. Optionally, filter products by a search query, or choose to have this task run hourly or daily.

Runs when a user triggers the task. Configuration includes sales channel name, only publish products matching this query, test mode, 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 name (required), only publish products matching this query, test mode (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 = result.data.publications.edges | map: "node" | where: "name", options.sales_channel_name__required | first %}
{% assign publication = publications | where: "name", options.sales_channel_name__required | first %}

{% if event.preview != true and publication == nil %}
  {"log": {{ publications | map: "name" | json }}}
  {"error": {{ options.sales_channel_name__required | prepend: "No sales channel found called " | json }}}
{% endif %}

{% assign cursor = nil %}
{% assign product_ids_to_publish = 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
            publishedOnPublication(
              publicationId: {{ publication.id | json }}
            )
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% for product_edge in result.data.products.edges %}
    {% assign product = product_edge.node %}
    {% unless product.publishedOnPublication %}
      {% assign product_ids_to_publish[product_ids_to_publish.size] = product.id %}
    {% endunless %}
  {% 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_to_publish[0] = "gid://shopify/Product/1234567890" %}
{% endif %}

{% if options.test_mode__boolean %}
  {% action "echo" %}
    {
      "message": "Found {{ product_ids_to_publish.size }} products to publish",
      "product_ids_to_publish": {{ product_ids_to_publish | json }}
    }
  {% endaction %}
{% else %}
  {% for product_id_to_publish in product_ids_to_publish %}
    {% action "shopify" %}
      mutation {
        publishablePublish(
          id: {{ product_id_to_publish | json }}
          input: {
            publicationId: {{ publication.id | 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.