Product Badges - New - based on real time

This script will output a New badge based on when the product is published. No longer need to be based on tag.

Change {% if date_difference < 1209600 %} number (in seconds) to the required badge show.

Change published_at to created_at to show new badge based on creation date.

Note New badge will show if product is turned off then turned back on again.

How it works:

  1. Will grab the product published date in seconds
  2. Compare that with today's date
  3. if the date difference in seconds then returns New badge
  4. Add tag 'no-new-badge' to overwrite the new badge
{% assign date_published = product.published_at | date:'%s' %}
{% assign date_now = 'now' | date:'%s' %}
{% assign date_difference = date_now | minus: date_published %}

{% comment %} less than or equal 14 days add new{% endcomment %}
    {% if date_difference < 1209600 %}
      {% unless product.tags contains "no-new-badge" %}
  		<span class="labels label-new">New</span>
      {% endunless %}
    {% endif %}

Leave a comment