Sync a tag for products that have a "compare at" price with Mechanic.

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

Sync a tag for products that have a "compare at" price

by Isaac Bowen (team@usemechanic.com)

A quick-and-useful utility task that scans your entire product catalog, adding the tag of your choice to products that have a "compare at" price, and removing it from products that don't.

Runs when some text is submitted.

15-day free trial – unlimited tasks

Documentation

Enter the tag of your choice, and Mechanic will scan your entire product catalog, adding the tag to any products with a "compare at" price, and removing the tag from any products that don't have a "compare at" price. (Any products that are already appropriately tagged or not tagged will be skipped.)

Note: This task performs these actions on-demand. To have these actions run automatically, try this auto-tagging 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 some text is submitted (mechanic/user/text)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          12345
        ],
        {
          "tags": "tag-of-your-choice"
        }
      ]
    }
  }
{% else %}
  {% assign compare_at_tag = event.data | strip %}

  {% for product in shop.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 has_tag = false %}
    {% if product_tags contains compare_at_tag %}
      {% assign has_tag = true %}
    {% endif %}

    {% capture product_tags_with_compare_at_tag %}{% if product.tags != blank %}{{ product.tags }}, {% endif %}{% unless has_tag %}{{ compare_at_tag }}{% endunless %}{% endcapture %}
    {% capture product_tags_without_compare_at_tag %}{% if has_tag %}{% capture tag_with_delimiters %}, {{ compare_at_tag }}, {% endcapture %}{% capture all_tags_with_delimiters %}, {{ product.tags }}, {% endcapture %}{{ all_tags_with_delimiters | replace: tag_with_delimiters, ", " }}{% else %}{{ product.tags }}{% endif %}{% endcapture %}

    {% if has_compare_at and has_tag == false %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "product",
              {{ product.id | json }}
            ],
            {
              "tags": {{ product_tags_with_compare_at_tag | json }}
            }
          ]
        }
      }
    {% elsif has_compare_at == false and has_tag %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "product",
              {{ product.id | json }}
            ],
            {
              "tags": {{ product_tags_without_compare_at_tag | json }}
            }
          ]
        }
      }
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.