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.
Try this taskWhen it runs
- Occurs whenever a product is created
- Occurs whenever a product is updated, ordered, or variants are added, removed or updated
- Occurs when a user manually triggers the task
What you choose
Apply this prefix to vendor tags
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.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/products/create
shopify/products/update
mechanic/user/trigger
How task subscriptions work
Task 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 %}
Mechanic Liquid reference