Auto-tag products without descriptions
This task runs when products are updated, checking if they have a description, and tagging them if not. Once a description has been added, then the tag will be removed.
Try this taskWhen it runs
- 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
Tag to apply
Documentation
This task runs when products are updated, checking if they have a description, and tagging them if not. Once a description has been added, then the tag will be removed.
You may also run the task manually to scan your entire product catalog,
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/update
mechanic/user/trigger
How task subscriptions work
Option types and flags
- Tag to apply (required)
Task code
{% assign tag_to_apply = options.tag_to_apply__required %}
{% assign products = array %}
{% if event.topic == "shopify/products/update" %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
id
descriptionHtml
tags
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign products[0] = result.data.product %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% assign cursor = nil %}
{% for n in (1..200) %}
{% capture query %}
query {
products(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
descriptionHtml
tags
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign products
= result.data.products.nodes
| default: array
| concat: products
%}
{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% if event.preview %}
{% assign products[0] = hash %}
{% assign products[0]["id"] = "gid://shopify/Product/1234567890" %}
{% endif %}
{% for product in products %}
{% if product.descriptionHtml == blank %}
{% unless product.tags contains tag_to_apply %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ product.id | json }}
tags: {{ tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% elsif product.tags contains tag_to_apply %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ product.id | json }}
tags: {{ tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference