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

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

Automatically publish and unpublish on a monthly cycle

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 Occurs when a user manually triggers the task and Occurs every day at midnight (in local time). Configuration includes resource type, resource id, 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

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.

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, 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/user/trigger
mechanic/scheduler/daily
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
resource type (required), resource id (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
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
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