Mechanic is a development and ecommerce automation platform for Shopify. :)
This advanced task allows you to schedule section publishing and unpublishing for your store. To use this task, enter a datetime to publish, a theme ID, and template names paired with one or more section IDs to publish or unpublish. If the configured datetime matches the current task run time at a scheduled 10 minute interval, then it will publish and/or unpublish the theme sections as configured.
Runs Occurs every 10 minutes, Occurs when a user manually triggers the task, and Occurs when a Mechanic action is performed. Configuration includes datetime to publish, theme id, template names and section ids to publish, and template names and section ids to unpublish.
This advanced task allows you to schedule section publishing and unpublishing for your store. To use this task, enter a datetime to publish, a theme ID, and template names paired with one or more section IDs to publish or unpublish. If the configured datetime matches the current task run time at a scheduled 10 minute interval, then it will publish and/or unpublish the theme sections as configured.
Important Notes:
- The datetime to publish must be in the format of "YYYY-MM-DD HH:MM" with a 24 hour clock time having minutes that are a multiple of 10, since that is the smallest scheduler interval available.
- Template names should be entered without the .json suffix (e.g. index, page.contact).
- Template names can appear in both the publish and unpublish keyval configurations, but they may only appear once in each since they are the left-hand keys.
- Section IDs should be entered in the right-hand value fields, paired with their respective template.
- Multiple section IDs being (un)published for the same template must be entered on multiple lines of the same entry field.
Example configuration
- Datetime to publish: 2022-07-31 13:30
- Theme ID: 1234567890
- Template names and section IDs to publish:
- index
- collage
- 123456abcdef
- Template names and section IDs to unpublish:
- index
- 234561bcdefa
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/10min mechanic/user/trigger mechanic/actions/perform
{% assign datetime_to_publish = options.datetime_to_publish__required %} {% assign theme_id = options.theme_id__number_required %} {% assign template_names_and_section_ids_to_publish = options.template_names_and_section_ids_to_publish__keyval_multiline %} {% assign template_names_and_section_ids_to_unpublish = options.template_names_and_section_ids_to_unpublish__keyval_multiline %} {% assign template_names = template_names_and_section_ids_to_publish | keys | default: array %} {% assign template_names = template_names_and_section_ids_to_unpublish | keys | default: array | concat: template_names | uniq %} {% if template_names == blank %} {% error "Configure at least one template name and section ID to publish or unpublish." %} {% break %} {% endif %} {% if event.topic contains "mechanic/scheduler/" or event.topic == "mechanic/user/trigger" %} {% if event.topic contains "mechanic/scheduler/" %} {% assign datetime = "now" | date: "%Y-%m-%d %H:%M" %} {% unless datetime_to_publish == datetime or event.preview %} {% log message: "Configured datetime to publish does not match run date; ending this task run.", run_date: datetime, datetime_to_publish: datetime_to_publish %} {% break %} {% endunless %} {% endif %} {% for template_name in template_names %} {% if template_name == blank %} {% continue %} {% endif %} {% assign section_ids_to_publish = template_names_and_section_ids_to_publish[template_name] | split: newline %} {% assign section_ids_to_unpublish = template_names_and_section_ids_to_unpublish[template_name] | split: newline %} {% log template_name: template_name, section_ids_to_publish: section_ids_to_publish, section_ids_to_unpublish: section_ids_to_unpublish %} {% capture template_path %}templates/{{ template_name }}.json{% endcapture %} {% action %} { "type": "shopify", "options": [ "get", "/admin/themes/{{ theme_id }}/assets.json?asset[key]={{ template_path }}" ], "meta": { "template_path": {{ template_path | json }}, "section_ids_to_publish": {{ section_ids_to_publish | json }}, "section_ids_to_unpublish": {{ section_ids_to_unpublish | json }} } } {% endaction %} {% endfor %} {% elsif event.topic == "mechanic/actions/perform" %} {% comment %} -- NOTE: Shopify will throw a 404 error on the GET if the asset is not accessible, halting that action, so no error checking is needed here {% endcomment %} {% assign template_path = action.meta.template_path %} {% assign section_ids_to_publish = action.meta.section_ids_to_publish %} {% assign section_ids_to_unpublish = action.meta.section_ids_to_unpublish %} {% unless template_path or event.preview %} {% comment %} -- stop processing if a meta value is not present (i.e. theme asset update actions) {% endcomment %} {% break %} {% endunless %} {% assign asset = action.run.result.asset %} {% if event.preview %} {% assign asset = hash %} {% assign asset["value"] = hash | json %} {% endif %} {% assign asset_value = asset.value | parse_json %} {% assign section_names = asset_value.sections | keys %} {% assign matched_sections_to_publish = array %} {% assign matched_sections_to_unpublish = array %} {% for section_name in section_names %} {% if section_ids_to_publish contains section_name %} {% assign matched_sections_to_publish = matched_sections_to_publish | push: section_name %} {% assign asset_value["sections"][section_name]["disabled"] = false %} {% elsif section_ids_to_unpublish contains section_name %} {% assign matched_sections_to_unpublish = matched_sections_to_unpublish | push: section_name %} {% assign asset_value["sections"][section_name]["disabled"] = true %} {% endif %} {% endfor %} {% log template_path: template_path, matched_sections_to_publish: matched_sections_to_publish, matched_sections_to_unpublish: matched_sections_to_unpublish %} {% unless matched_sections_to_publish != blank or matched_sections_to_unpublish != blank or event.preview %} {% log "No matched sections to publish or unpublish; no update will be made." %} {% break %} {% endunless %} {% action "shopify" %} [ "put", "/admin/themes/{{ theme_id }}/assets.json", { "asset": { "key": {{ template_path | json }}, "value": {{ asset_value | json | json }} } } ] {% endaction %} {% endif %}