Mechanic Get Mechanic
Task library

Unpublish a certain collection, daily

Use this task with the companion publishing 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 18 hour delay
  • Occurs every Tuesday at midnight (in local time), with a 18 hour delay
  • Occurs every Wednesday at midnight (in local time), with a 18 hour delay
Show 4 more
  • Occurs every Thursday at midnight (in local time), with a 18 hour delay
  • Occurs every Friday at midnight (in local time), with a 18 hour delay
  • Occurs every Saturday at midnight (in local time), with a 14 hour delay
  • Occurs every Sunday at midnight (in local time), with a 14 hour delay

What you choose

Collection ID

Documentation

Use this task with the companion publishing 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 Publish a certain collection, daily, from our task library.

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

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

Note: This task only unpublishes products from 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+18.hours
mechanic/scheduler/tuesday+18.hours
mechanic/scheduler/wednesday+18.hours
mechanic/scheduler/thursday+18.hours
mechanic/scheduler/friday+18.hours
mechanic/scheduler/saturday+14.hours
mechanic/scheduler/sunday+14.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 published 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": "2020-12-12T12:00:00Z"
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign product_ids
    = result.data.collection.products.nodes
    | where: "publishedAt"
    | map: "id"
    | concat: product_ids
  %}

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

{% comment %}
  -- unpublish qualifying products from online store
{% endcomment %}

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