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

Mechanic is a development platform for Shopify. :)

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

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. If you're using Shopify discounts, this can allow you to use automatic sale collections – based on these tags – to control eligibility for your discounts.

Runs when a product is created, when a product is updated, and when a user triggers the task. Configuration includes tag for sale products, tag for all other products, and sale products must have a price lower than the compare at price.

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. If you're using Shopify discounts, this can allow you to use automatic sale collections – based on these tags – to control eligibility for your discounts.

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.

Open source
View on GitHub to contribute to this task
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, sale products must have a price lower than the compare at price (boolean)
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.preview %}
  {% capture products_json %}
    [
      {
        "admin_graphql_api_id": "gid://shopify/Product/1234567890",
        "tags": "",
        "variants": [
          {
            "price": "19.99",
            "compare_at_price": "24.99"
          }
        ]
      }
    ]
  {% endcapture %}

  {% assign products = products_json | parse_json %}
{% elsif 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_price = false %}
  {% assign has_sale_price = false %}

  {% for variant in product.variants %}
    {% if variant.compare_at_price == blank %}
      {% continue %}
    {% endif %}

    {% assign has_compare_at_price = true %}

    {% if options.sale_products_must_have_a_price_lower_than_the_compare_at_price__boolean %}
      {% assign price = variant.price | times: 1 %}
      {% assign compare_at_price = variant.compare_at_price | times: 1 %}
      {% if price < compare_at_price %}
        {% assign has_sale_price = true %}
      {% endif %}
    {% endif %}
  {% endfor %}

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

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

  {% assign product_qualifies_as_sale = false %}
  {% if has_compare_at_price %}
    {% if options.sale_products_must_have_a_price_lower_than_the_compare_at_price__boolean %}
      {% if has_sale_price %}
        {% assign product_qualifies_as_sale = true %}
      {% endif %}
    {% else %}
      {% assign product_qualifies_as_sale = true %}
    {% endif %}
  {% endif %}

  {% if product_qualifies_as_sale %}
    {% 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 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
Sale products must have a price lower than the compare at price
true