Send an email when a product's price goes below its cost, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Send an email when a product's price goes below its cost

by Isaac Bowen (team@usemechanic.com)

This task watches for product updates, and sends an email for each variant that's found to have a unit cost that's greater than the variant's for-sale price.

Runs when a product is updated. Configuration includes recipient email.

15-day free trial – unlimited tasks

Documentation

This task watches for product updates, and sends an email (to the recipient of your choice) for each variant that's found to have a unit cost that's greater than the variant's for-sale price. It will only send one email per variant, per observed change to variant cost. If a product is updated that the task hasn't had a chance to previously observe, emails will be sent immediately for any variants that have a unit cost greater than their price.

YouTube: Watch the development video!

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 a product is updated (shopify/products/update)
Options
recipient email (email, required)
Script
{% capture query %}
  query {
    product(id: {{ product.admin_graphql_api_id | json }}) {
      metafield(
        namespace: "mechanic"
        key: "variant_costs"
      ) {
        id
        value
      }
      variants(first: 250) {
        edges {
          node {
            id
            legacyResourceId
            sku
            title
            price
            inventoryItem {
              unitCost {
                amount
              }
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "product": {
          "metafield": null,
          "variants": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/ProductVariant/1234567890",
                  "legacyResourceId": "1234567890",
                  "price": "5.00",
                  "sku": "ABC123",
                  "title": "Default Title",
                  "inventoryItem": {
                    "unitCost": {
                      "amount": "10.0"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign known_costs = result.data.product.metafield.value | default: "{}" | parse_json %}
{% assign current_costs = hash %}

{% for variant_edge in result.data.product.variants.edges %}
  {% assign unit_cost = variant_edge.node.inventoryItem.unitCost.amount | times: 1 %}
  {% assign price = variant_edge.node.price | times: 1 %}

  {% assign current_costs[variant_edge.node.id] = unit_cost %}

  {% if price < unit_cost and known_costs[variant_edge.node.id] != unit_cost %}
    {% assign variant_name = variant_edge.node.sku | default: variant_edge.node.title %}
    {% if variant_name == "Default Title" %}
      {% assign variant_name = product.title %}
    {% endif %}

    {% capture email_subject %}
      {{ variant_name }} was priced lower than its cost
    {% endcapture %}

    {% capture email_body %}
      Hello,

      {{ variant_name }} was found to have a unit cost of {{ unit_cost | times: 100 | money_with_currency }}, while its price is set to {{ price | times: 100 | money_with_currency }}.

      <a href="https://{{ shop.myshopify_domain }}/admin/products/{{ product.id }}/variants/{{ variant_edge.node.legacyResourceId }}">Manage this in Shopify</a>

      Thanks,
      Mechanic, for {{ shop.name }}
    {% endcapture %}

    {% action "email" %}
      {
        "to": {{ options.recipient_email__email_required | json }},
        "subject": {{ email_subject | strip | json }},
        "body": {{ email_body | unindent | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}
  {% endif %}
{% endfor %}

{% if known_costs != current_costs %}
  {% action "shopify" %}
    mutation {
      productUpdate(
        input: {
          id: {{ product.admin_graphql_api_id | json }}
          metafields: [
            {
              id: {{ result.data.product.metafield.id | json }}
              namespace: "mechanic"
              key: "variant_costs"
              valueType: JSON_STRING
              value: {{ current_costs | json | json }}
            }
          ]
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.