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

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

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

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.

Runs Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or 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.

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, 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/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
recipient email (email, required)
Code
{% capture query %}
  query {
    product(id: {{ product.admin_graphql_api_id | json }}) {
      variants(first: 250) {
        edges {
          node {
            id
            legacyResourceId
            sku
            title
            price
            inventoryItem {
              unitCost {
                amount
              }
            }
            known_cost_and_price: metafield(
              namespace: "mechanic"
              key: "known_cost_and_price"
            ) {
              id
              value
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

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

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

{% assign product = result.data.product %}
{% assign variants = product.variants.edges | map: "node" %}

{% assign metafields = array %}

{% for variant in variants %}
  {% assign unit_cost = variant.inventoryItem.unitCost.amount | times: 1 %}
  {% assign price = variant.price | times: 1 %}
  {% assign known_cost_and_price = variant.known_cost_and_price.value | default: "{}" | parse_json %}

  {% assign current_cost_and_price = hash %}
  {% assign current_cost_and_price["cost"] = unit_cost %}
  {% assign current_cost_and_price["price"] = price %}

  {% log
    variant_id: variant.id,
    known_cost_and_price: known_cost_and_price,
    current_cost_and_price: current_cost_and_price
  %}

  {% if price >= unit_cost and variant.known_cost_and_price != blank %}
    {% action "shopify" %}
      mutation {
        metafieldDelete(
          input: {
            id: {{ variant.known_cost_and_price.id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

  {% elsif price < unit_cost and known_cost_and_price != current_cost_and_price %}
    {% assign variant_name = variant.sku | default: variant.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 | currency }}, while its price is set to {{ price | currency }}.

      <a href="https://{{ shop.myshopify_domain }}/admin/products/{{ product.id }}/variants/{{ variant.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 %}

    {% capture metafield %}
      {
        ownerId: {{ variant.id | json }}
        namespace: "mechanic"
        key: "known_cost_and_price"
        type: "json"
        value: {{ current_cost_and_price | json | json }}
      }
    {% endcapture %}

    {% assign metafields = metafields | push: metafield %}
  {% endif %}
{% endfor %}

{% assign groups_of_metafields = metafields | in_groups_of: 25, fill_with: false %}

{% for group_of_metafields in groups_of_metafields %}
  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {{ group_of_metafields | join: newline }}
        ]
      ) {
        metafields {
          id
          namespace
          key
          type
          value
          owner {
            ... on ProductVariant {
              id
            }
          }
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more