Auto-tag products when a "compare at" price is added or removed with Mechanic.

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

Auto-tag products when a "compare at" price is added or removed

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 Mechanic will take care of tagging products when you give them a "compare at" price, and untagging those products when the "compare at" price is removed.

Runs when a product is created and when a product is updated. Configuration includes product tag.

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 Mechanic will take care of tagging products when you give them a "compare at" price, and untagging those products when the "compare at" price is removed.

Note: This task performs these actions when products are created or updated. To do a one-time sync of your existing product catalog, try this task!

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)
Options
product tag (required)
Script
{% 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 has_tag = false %}
{% if product_tags contains options.product_tag__required %}
  {% assign has_tag = true %}
{% endif %}

{% capture product_tags_with_tag %}{% if product.tags != blank %}{{ product.tags }}, {% endif %}{% unless has_tag %}{{ options.product_tag__required }}{% endunless %}{% endcapture %}
{% capture product_tags_without_tag %}{% if has_tag %}{% capture tag_with_delimiters %}, {{ options.product_tag__required }}, {% endcapture %}{% capture all_tags_with_delimiters %}, {{ product.tags }}, {% endcapture %}{{ all_tags_with_delimiters | replace: tag_with_delimiters, ", " }}{% else %}{{ product.tags }}{% endif %}{% endcapture %}

{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          {{ product.id | json }}
        ],
        {
          "tags": {{ product_tags_with_tag | json }}
        }
      ]
    }
  }
{% elsif has_compare_at and has_tag == false %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          {{ product.id | json }}
        ],
        {
          "tags": {{ product_tags_with_tag | json }}
        }
      ]
    }
  }
{% elsif has_compare_at and has_tag %}
  { "log": "No action required: this product has a compare-at price, but is already tagged." }
{% elsif has_compare_at == false and has_tag %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          {{ product.id | json }}
        ],
        {
          "tags": {{ product_tags_without_tag | json }}
        }
      ]
    }
  }
{% elsif has_compare_at == false and has_tag == false %}
  { "log": "No action required: this product has no compare-at price, and does not have the compare-at tag." }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.