Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to receive an immediate email alert, whenever a variant's total inventory hits 0, when added up across all locations. You can limit alerts to a specific collection or publication (e.g. Online Store or Point of Sale).
Runs Occurs whenever an inventory level is updated and Occurs when a user manually triggers the task. Configuration includes must belong to collection id, must be published to publication id, email subject, email body, and email recipient.
Use this task to receive an immediate email alert, whenever a variant's total inventory hits 0, when added up across all locations. You can limit alerts to a specific collection or publication (e.g. Online Store or Point of Sale).
Notes:
- You can filter by collection or publication, not both at the same time.
- Run the task manually to get the list of publication IDs for your shop.
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?)
shopify/inventory_levels/update mechanic/user/trigger
{% assign must_belong_to_collection_id = options.must_belong_to_collection_id %} {% assign must_be_published_to_publication_id = options.must_be_published_to_publication_id %} {% assign email_subject = options.email_subject__required %} {% assign email_body = options.email_body__multiline_required %} {% assign email_recipient = options.email_recipient__email_required %} {% if must_belong_to_collection_id != blank and must_be_published_to_publication_id != blank %} {% error "You can only filter by collection or publication, not both." %} {% endif %} {% if event.topic == "mechanic/user/trigger" %} {% comment %} -- run the task manually to get the list of your publications and their IDs {% endcomment %} {% capture query %} query { publications( first: 250 catalogType:APP ) { nodes { id catalog { ... on AppCatalog { apps(first: 1) { nodes { title } } } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "publications": { "nodes": [ { "id": "gid://shopify/Publication/1234567890", "catalog": { "apps": { "nodes": [ { "title": "Online Store" } ] } } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign publication_names_by_id = hash %} {% for publication in result.data.publications.nodes %} {% assign publication_id = publication.id | remove: "gid://shopify/Publication/" %} {% assign publication_names_by_id[publication_id] = publication.catalog.apps.nodes.first.title %} {% endfor %} {% log publication_names_by_id %} {% elsif event.topic == "shopify/inventory_levels/update" %} {% capture query %} query { inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) { item { variant { displayName inventoryQuantity legacyResourceId product { legacyResourceId {% if must_belong_to_collection_id -%} inCollection(id: "gid://shopify/Collection/{{must_belong_to_collection_id}}") {%- elsif must_be_published_to_publication_id -%} publishedOnPublication(publicationId: "gid://shopify/Publication/{{must_be_published_to_publication_id}}") {%- endif %} } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "inventoryLevel": { "item": { "variant": { "displayName": "Short sleeve t-shirt - L / Red", "inventoryQuantity": 0, "legacyResourceId": "1234567890", "product": { "legacyResourceId": "1234567890" {% if must_belong_to_collection_id -%} , "inCollection" : "true" {%- elsif options.must_be_published_to_publication_id -%} , "publishedOnPublication": "true" {%- endif %} } } } } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign variant = result.data.inventoryLevel.item.variant %} {% assign product_qualifies = false %} {% if must_belong_to_collection_id == blank and must_be_published_to_publication_id == blank %} {% assign product_qualifies = true %} {% elsif must_belong_to_collection_id and variant.product.inCollection %} {% assign product_qualifies = true %} {% elsif must_be_published_to_publication_id and variant.product.publishedOnPublication %} {% assign product_qualifies = true %} {% endif %} {% if variant.inventoryQuantity == 0 and product_qualifies %} {% assign variant_admin_url = "https://" | append: shop.domain | append: "/admin/products/" | append: variant.product.legacyResourceId | append: "/variants/" | append: variant.legacyResourceId %} {% assign email_subject = email_subject | replace: "VARIANT_TITLE", variant.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %} {% assign email_body = email_body | replace: "VARIANT_TITLE", variant.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %} {% action "email" %} { "to": {{ email_recipient | json }}, "subject": {{ email_subject | json }}, "body": {{ email_body | strip | newline_to_br | json }}, "reply_to": {{ shop.customer_email | json }}, "from_display_name": {{ shop.name | json }} } {% endaction %} {% endif %} {% endif %}
"VARIANT_TITLE" has hit 0 inventory
Hello, The variant "VARIANT_TITLE" is now at 0 inventory, totaled across all locations. <a href="VARIANT_ADMIN_URL">Manage this variant in Shopify</a> Thanks, Mechanic, for {{ shop.name }}