Automatically publish and unpublish on a monthly cycle, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Automatically publish and unpublish on a monthly cycle

by Isaac Bowen (team@usemechanic.com)

Use this task to make a single product or page available for a limited window of time, on a monthly basis. Redirect your visitors to a specific page when that resource is unavailable.

Runs when a user triggers the task and every day at midnight. Configuration includes resource type, resource, day of the month to publish resource, day of the month to unpublish resource, and redirect url when resource is not published.

15-day free trial – unlimited tasks

Documentation

Configure this task using the resource type (either "product" or "page"), a resource ID (learn how to find this), the days of the month for publishing and unpublishing, and the URL you'd like to send visitors to when they land on the resource when it's unpublished.

This task will run automatically, at midnight in your store's local timezone. Run this task manually, using the "Run task" button, if you'd like to test your settings immediately. :)

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)
every day at midnight (mechanic/scheduler/daily)
Options
resource type (required), resource (number, required), day of the month to publish resource (number, required), day of the month to unpublish resource (number, required), redirect url when resource is not published
Script
{% if options.resource_type__required != "product" and options.resource_type__required != "page" %}
  {"error": "Set 'Resource type' to either 'product' or 'page'."}
{% endif %}

{% assign dom = "now" | date: "%-d" | times: 1 %}
{% assign resource_collection = options.resource_type__required | append: "s" %}
{% assign resource = shop[resource_collection][options.resource_id__number_required] %}

{% assign publish_dom = options.day_of_the_month_to_publish_resource__number_required %}
{% assign unpublish_dom = options.day_of_the_month_to_unpublish_resource__number_required %}

{% action "echo" resource_collection, resource.handle, resource.published_at, dom, publish_dom, unpublish_dom %}

{% if publish_dom < unpublish_dom and publish_dom <= dom and dom < unpublish_dom %}
  {% assign should_be_published = true %}
{% elsif unpublish_dom < publish_dom and publish_dom <= dom %}
  {% assign should_be_published = true %}
{% elsif unpublish_dom < publish_dom and unpublish_dom <= dom and dom <= publish_dom %}
  {% assign should_be_published = false %}
{% elsif publish_dom < unpublish_dom and unpublish_dom <= dom  %}
  {% assign should_be_published = false %}
{% elsif publish_dom < unpublish_dom and dom < publish_dom %}
  {% assign should_be_published = false %}
{% endif %}

{% action "echo" should_be_published %}

{% if event.preview or should_be_published == true %}
  {% if resource.published_at != blank %}
    {"log": "Resource is already published; nothing to do."}
  {% else %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            {{ options.resource_type__required | json }},
            {{ resource.id | json }}
          ],
          {
            "published": true
          }
        ]
      }
    }
  {% endif %}
{% elsif should_be_published == false %}
  {% if resource.published_at == blank %}
    {"log": "Resource is already unpublished; nothing to do."}
  {% else %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            {{ options.resource_type__required | json }},
            {{ resource.id | json }}
          ],
          {
            "published": false
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Online store JavaScript
{% if options.redirect_url_when_resource_is_not_published != blank %}
  {% assign resource_collection = options.resource_type__required | append: "s" %}
  {% assign resource = shop[resource_collection][options.resource_id__number_required] %}

  if (document.title.match(/404 Not Found/) && window.location.pathname.match({{ "/" | append: options.resource_type__required | append: "s/" | append: resource.handle | json }})) {
    window.location.href = {{ options.redirect_url_when_resource_is_not_published | json }};
  }
{% endif %}
When this task is installed, this code is loaded into the online storefront using a ScriptTag.
Defaults
Resource type
product