Mechanic Get Mechanic
Task library

Publish a certain collection, daily

Use this task with the companion unpublishing task (see this task's documentation) to make a certain collection of products available on a schedule, and unavailable on another schedule.

Try this task

When it runs

  • Occurs every Monday at midnight (in local time), with a 9 hour delay
  • Occurs every Tuesday at midnight (in local time), with a 9 hour delay
  • Occurs every Wednesday at midnight (in local time), with a 9 hour delay
Show 4 more
  • Occurs every Thursday at midnight (in local time), with a 9 hour delay
  • Occurs every Friday at midnight (in local time), with a 9 hour delay
  • Occurs every Saturday at midnight (in local time), with a 10 hour delay
  • Occurs every Sunday at midnight (in local time), with a 10 hour delay

What you choose

Collection ID

Documentation

Use this task with the companion unpublishing task (see this task's documentation) to make a certain collection of products available on a schedule, and unavailable on another schedule.

For best results, pair this task with Unpublish a certain collection, daily, from our task library.

To configure this task, enter the "Collection ID" for the collection you wish to publish. Refer to this article for assistance on finding Shopify resource IDs.

Out of the box, this task will publish everything in this collection at 9am on weekdays, and 10am on weekends. To adjust this, click the "Show Advanced" link, and tweak the "Subscriptions" setting to taste. Each line represents midnight in your shop's local timezone, plus whatever number of hours you prefer. For example, mechanic/scheduler/tuesday+13.hours would mean Tuesday at 1pm.

Note: This task only publishes products to the 'Online Store'.

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/scheduler/monday+9.hours
mechanic/scheduler/tuesday+9.hours
mechanic/scheduler/wednesday+9.hours
mechanic/scheduler/thursday+9.hours
mechanic/scheduler/friday+9.hours
mechanic/scheduler/saturday+10.hours
mechanic/scheduler/sunday+10.hours
How task subscriptions work

Option types and flags

Task code

{% assign collection_id = options.collection_id__required %}

{% comment %}
  -- get online store publication ID
  -- Note: title filter does work for app catalog titles, so have to get all publications
{% 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": "Online Store"
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign online_store_publication_id = nil %}

{% for publication in result.data.publications.nodes %}
  {% assign publication_name = publication.catalog.apps.nodes.first.title %}

  {% if publication_name == "Online Store" %}
    {% assign online_store_publication_id = publication.id %}
  {% endif %}
{% endfor %}

{% comment %}
  -- get IDs of unpublished products in the collection
{% endcomment %}

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

{% for n in (1..100) %}
  {% capture query %}
    query {
      collection(id: {{ collection_id | prepend: "gid://shopify/Collection/" | json }}) {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            publishedAt
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "collection": {
            "products": {
              "nodes": [
                {
                  "id": "gid://shopify/Product/1234567890",
                  "publishedAt": null
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% for product in result.data.collection.products.nodes %}
    {% if product.publishedAt == blank %}
      {% assign product_ids = product_ids | push: product.id %}
    {% endif %}
  {% endfor %}

  {% if result.data.collection.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.collection.products.pageInfo.endCursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% comment %}
  -- publish qualifying products to the online store
{% endcomment %}

{% for product_id in product_ids %}
  {% action "shopify" %}
    mutation {
      publishablePublish(
        id: {{ product_id | json }}
        input: {
          publicationId: {{ online_store_publication_id | json }}
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Mechanic Liquid reference