Mechanic Get Mechanic
Task library

Schedule a storefront banner

Use this task to publish a banner, to be displayed across your online storefront. This task comes with some pre-configured HTML and CSS – customize to taste. :)

Try this task

What you choose

  • Starting date and time
  • Ending date and time
  • Banner HTML
  • Banner parent CSS selector

Documentation

Use this task to publish a banner, to be displayed across your online storefront. This task comes with some pre-configured HTML and CSS – customize to taste. :)

Enable Mechanic in your theme

This task needs to run JavaScript in your online store. If Mechanic shows a theme setup prompt, click Enable Mechanic in your theme, make sure Mechanic's Online store JavaScript is turned on under App embeds, and click Save in the theme editor. Then return to Mechanic to confirm setup.

You only need to do this once for your current theme; the same setting serves all your enabled tasks that use storefront JavaScript. You can leave it enabled if you disable this task. If your store doesn't already have Mechanic loading JavaScript, this task's storefront functionality won't work until setup is complete. Learn about theme setup and existing tasks.

Configure the banner

Configure the starting and ending times as appropriate, remembering that all times in Mechanic are local to your store's timezone.

Adjust (or replace!) the banner HTML and CSS to taste. Mechanic will place your code within a plain <div> wrapper. To control where your banner is inserted, use the "Banner parent CSS selector" option. Mechanic will insert your banner as the first child of whatever element is matched by that CSS selector.

Please note: the Mechanic support crew can't help out with banner design.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

How task subscriptions work

Option types and flags

Task code

Mechanic Liquid reference

Online store JavaScript

This code runs after the storefront page loads. Enable Mechanic’s Online store JavaScript app embed in your published theme and save the theme to finish setup.

var now = Date.now();
var startAt = {{ options.starting_date_and_time__required | date: "%s" | times: 1000 | json }};
var endAt = {{ options.ending_date_and_time__required | date: "%s" | times: 1000 | json }};

if (document.getElementById('admin-bar-iframe')) {
  if (startAt === 0) {
    alert('Scheduled banner with Mechanic: starting time was not understood');
  }
  if (endAt === 0) {
    alert('Scheduled banner with Mechanic: ending time was not understood');
  }
}

if (startAt !== 0 && endAt !== 0 && startAt <= now && now <= endAt) {
  var bannerContainer = document.createElement('div');
  bannerContainer.innerHTML = {{ options.banner_html__required_multiline_code | json }};

  var parentCssSelector = {{ options.banner_parent_css_selector__required | json }};
  var parent = document.querySelector(parentCssSelector);

  parent.insertBefore(bannerContainer, parent.firstChild);
}

Default option values

Starting date and time
May 1 2019 12:00am
Ending date and time
June 1 2019 12:00am
Banner HTML
<style>
#banner_placeholder {
    height: 50px;
    width: 100%;
}

#banner {
    position: absolute;
    z-index: 1000;
    top: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    color: #ddd;
    padding: 10px;
    font-size: 16px;
}

#banner a {
    font-weight: bold;
    text-decoration: underline;
    color: #ddd;
}

#banner a:hover {
    color: #fff;
}
</style>

<div id="banner_placeholder"></div>
<div id="banner">
  It's time for a banner! <a href="/">Keep shopping!</a>
</div>
Banner parent CSS selector
body