Mechanic Get Mechanic
Task library

Scheduled theme publishing

This task will allow you to schedule any number of themes to be published at future dates and times. When the task runs at its normally scheduled 10 minute interval, it will find the entry with the most recent configured date in the past, and if that theme is not currently published, then the task will publish it.

Try this task

When it runs

  • Occurs every 10 minutes
  • Occurs when a user manually triggers the task

What you choose

Theme IDs and publish dates

Documentation

This task will allow you to schedule any number of themes to be published at future dates and times. When the task runs at its normally scheduled 10 minute interval, it will find the entry with the most recent configured date in the past, and if that theme is not currently published, then the task will publish it.

Within the Theme IDs and publish dates field, simply configure the theme IDs on the left along with the related publish dates on the right. Publish dates may be entered with a date and time, using a 24-hour clock, in the format YYYY-MM-DD HH:MM, or only with a date in the format YYYY-MM-DD, in which case the time would default to midnight.

Important notes:
- All times are in local shop time.
- The task will attempt to parse the configured dates and times, and will display an error at run time if any of them are unparsable in the expected format.
- While this task is active it may override any manual publishing actions.

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/10min
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign theme_ids_and_publish_dates = options.theme_ids_and_publish_dates__keyval %}

{% if event.preview %}
  {% assign theme_to_publish = hash %}
  {% assign theme_to_publish["id"] = 1234567890 %}

{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign shop_theme_ids = shop.themes | map: "id" %}

  {% assign publication_candidates = array %}

  {% for keyval in theme_ids_and_publish_dates %}
    {% assign theme_id = keyval.first %}
    {% assign publish_date = keyval.last %}

    {% assign theme = shop.themes[theme_id] %}

    {% if theme == blank %}
      {% error
        message: "A theme with this id was not found in this shop.",
        theme_id: theme_id,
        shop_theme_ids: shop_theme_ids
      %}
    {% endif %}

    {% assign valid_publish_date = publish_date | parse_date: "%Y-%m-%d %H:%M" %}

    {% unless valid_publish_date %}
      {% assign valid_publish_date = publish_date | parse_date: "%Y-%m-%d" %}
    {% endunless %}

    {% if valid_publish_date == blank %}
      {% error
        message: "A theme publish date could not be parsed. Refer to the task documentation for valid date/time formats.",
        theme_id: theme_id,
        publish_date: publish_date
      %}
    {% endif %}

    {% assign publication_candidate = hash %}
    {% assign publication_candidate["theme"] = theme %}
    {% assign publication_candidate["publish_date"] = valid_publish_date | date: "%s" %}
    {% assign publication_candidates = publication_candidates | push: publication_candidate %}
  {% endfor %}

  {% assign publication_candidates = publication_candidates | sort: "publish_date" %}

  {% assign now = "now" | date: "%s" %}
  {% assign theme_to_publish = nil %}

  {% for publication_candidate in publication_candidates %}
    {% if publication_candidate.publish_date <= now %}
      {% assign theme_to_publish = publication_candidate.theme %}

    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if theme_to_publish != blank %}
  {% if theme_to_publish.role == "main" %}
    {% log
      message: "The theme to be published is already set as the main theme on the shop; skipping.",
      theme_to_publish: theme_to_publish
    %}

  {% else %}
    {% action "shopify" %}
      [
        "update",
        "theme",
        {
          "id": {{ theme_to_publish.id }},
          "role": "main"
        }
      ]
    {% endaction %}
  {% endif %}

{% else %}
  {% log "No themes qualify to be published based on their configured publish dates." %}
{% endif %}
Mechanic Liquid reference