Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to automatically roll out your products on specific days of the week. This task runs every midnight, in your shop's local timezone, and it scans your catalog for unpublished products tagged with the current day of the week (e.g. "Monday", "tuesday", etc).
Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task.
Use this task to automatically roll out your products on specific days of the week. This task runs every midnight, in your shop's local timezone, and it scans your catalog for unpublished products tagged with the current day of the week (e.g. "Monday", "tuesday", etc).
You can also run this task manually, to publish any unpublished products tagged with the current day of the week.
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?)
mechanic/scheduler/daily mechanic/user/trigger
{% assign day_of_week_lowercase = "now" | date: "%A" | downcase %}
{% 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 all unpublished products with the day of week tag (tag search filter is case-insensitive, but using lowercase for tag verification in results)
{% endcomment %}
{%- capture search_query -%}
-published_at:* tag:{{ day_of_week_lowercase | json }}
{%- endcapture -%}
{% assign cursor = nil %}
{% assign products = array %}
{% for n in (1..100) %}
{% capture query %}
query {
products(
first: 250
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
tags
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"products": {
"nodes": [
{
"id": "gid://shopify/Product/1234567890",
"tags": {{ array | push: day_of_week_lowercase | json }}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign products = products | concat: result.data.products.nodes %}
{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if products == blank %}
{% log "No unpublished products qualified to be published on this task run." %}
{% endif %}
{% for product in products %}
{% comment %}
-- validate the product does indeed have the day of week tag exactly, since the tag query filter returns partial matches
{% endcomment %}
{% assign product_tags_lowercase
= product.tags
| json
| downcase
| parse_json
%}
{% if product_tags_lowercase contains day_of_week_lowercase %}
{% action "shopify" %}
mutation {
publishablePublish(
id: {{ product.id | json }}
input: {
publicationId: {{ online_store_publication_id | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}