Auto-tag products with their vendors, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Auto-tag products with their vendors

Use this task to copy each product's vendor to the product's tag. Optionally, you can apply a prefix to all vendor tags (e.g. "vendor-"). Run this task manually to scan every product in your store. Otherwise, this task will run for specific products, whenever a product is created or updated.

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 apply this prefix to vendor tags.

15-day free trial – unlimited tasks

Documentation

Use this task to copy each product's vendor to the product's tag. Optionally, you can apply a prefix to all vendor tags (e.g. "vendor-"). Run this task manually to scan every product in your store. Otherwise, this task will run for specific products, whenever a product is created or updated.

Note: When using the vendor tag prefix option, any tags on a product that contain that prefix, but do not match the current vendor, will be removed. This does not happen when the prefix option is not used or has been changed.

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
apply this prefix to vendor tags
Code
{% assign vendor_tag_prefix = options.apply_this_prefix_to_vendor_tags %}

{% if event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}
  {% assign products = array %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              tags
              vendor
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% assign products_result = result.data.products.edges | map: "node" | where: "vendor" %}
    {% assign products = products | concat: products_result %}

    {% if result.data.products.pageInfo.hasNextPage %}
      {% assign cursor = result.data.products.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

{% elsif event.topic contains "shopify/products/" %}
  {% if product.vendor == blank %}
    {% break %}
  {% endif %}

  {% assign graphql_product = hash %}
  {% assign graphql_product["id"] = product.admin_graphql_api_id %}
  {% assign graphql_product["tags"] = product.tags | split: ", " %}
  {% assign graphql_product["vendor"] = product.vendor %}

  {% assign products = array %}
  {% assign products[0] = graphql_product %}
{% endif %}

{% if event.preview %}
  {% capture products_json %}
    [
      {
        "id": "gid://shopify/Product/1234567890",
        "vendor": "ACME"
      }
    ]
  {% endcapture %}

  {% assign products = products_json | parse_json %}
{% endif %}

{% for product in products %}
  {% assign tags_to_remove = array %}
  {% assign vendor_tag = product.vendor %}

  {% if vendor_tag_prefix != blank %}
    {% assign vendor_tag = vendor_tag | prepend: vendor_tag_prefix %}

    {% for tag in product.tags %}
      {% if tag contains vendor_tag_prefix and tag != vendor_tag %}
        {% assign trimmed_tag = tag | slice: 0, vendor_tag_prefix.size %}
        
        {% if trimmed_tag == vendor_tag_prefix %}
          {% assign tags_to_remove = tags_to_remove | push: tag %}
        {% endif %}
      {% endif %}
    {% endfor %}

    {% if tags_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tags_to_remove | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}      
    {% endif %}
  {% endif %}

  {% unless product.tags contains vendor_tag %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ product.id | json }}
          tags: {{ vendor_tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more