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

Mechanic is a development and ecommerce automation 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 Occurs whenever a product is created, Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated, and Occurs when a user manually 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, 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?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/products/create
shopify/products/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag for sale products, tag for all other products, sale products must have a price lower than the compare at price (boolean)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
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