Scheduled theme publishing, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs every 10 minutes and Occurs when a user manually triggers the task. Configuration includes theme ids and publish dates.

15-day free trial – unlimited tasks

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.

Developer details

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

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/scheduler/10min
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
theme ids and publish dates (keyval)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more