Auto-tag products that have a "compare at" price with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag products that have a "compare at" price

by Isaac Bowen (team@usemechanic.com)

This task will keep your sale tags in sync, without any manual work. Configure the task with a tag to apply (and optionally a tag for products that aren't on sale), and Mechanic will take care of applying and removing tags as appropriate.

Runs when a product is created, when a product is updated, and when a user triggers the task. Configuration includes tag for sale products and tag for all other products.

15-day free trial – unlimited tasks

Documentation

This task will keep your sale tags in sync, without any manual work. Configure the task with a tag to apply (and optionally a tag for products that aren't on sale), and Mechanic will take care of applying and removing tags as appropriate.

Run this task manually to update your entire product catalog at once.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
when a user triggers the task (mechanic/user/trigger)
Options
tag for sale products, tag for all other products
Script
{% if options.tag_for_sale_products == blank and options.tag_for_all_other_products == blank %}
  {"error": "Please fill in at least one of the two tag options."}
{% endif %}

{% if event.topic contains "shopify/products/" %}
  {% assign products = array %}
  {% assign products[0] = product %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign products = shop.products %}
{% endif %}

{% for product in products %}
  {% assign has_compare_at = false %}
  {% for variant in product.variants %}
    {% if variant.compare_at_price != nil %}
      {% assign has_compare_at = true %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% assign product_tags = product.tags | split: ", " %}

  {% assign tag_to_add = nil %}
  {% assign tag_to_remove = nil %}

  {% if has_compare_at %}
    {% unless product_tags contains options.tag_for_sale_products %}
      {% assign tag_to_add =        options.tag_for_sale_products %}
    {% endunless %}

    {% if product_tags contains options.tag_for_all_other_products %}
      {% assign tag_to_remove = options.tag_for_all_other_products %}
    {% endif %}
  {% else %}
    {% if product_tags contains options.tag_for_sale_products %}
      {% assign tag_to_remove = options.tag_for_sale_products %}
    {% endif %}

    {% unless product_tags contains options.tag_for_all_other_products %}
      {% assign tag_to_add =        options.tag_for_all_other_products %}
    {% endunless %}
  {% endif %}

  {% if event.preview %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: "gid://shopify/Product/1234567890"
          tags: {{ options.tag_for_sale_products | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% elsif tag_to_add or tag_to_remove %}
    {% action "shopify" %}
      mutation {
        {% if tag_to_add %}
          tagsAdd(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ tag_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}

        {% if tag_to_remove %}
          tagsRemove(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ tag_to_remove | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tag for sale products
on-sale
Tag for all other products
not-on-sale