Email all customers who recently purchased a certain product with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Email all customers who recently purchased a certain product

by Isaac Bowen (team@usemechanic.com)

Need to reach customers who purchased something specific? This task scans your order history over the last 60 days, looking for paid orders for the product you specify, and sends each one an email that you can customize.

Runs when some text is submitted. Configuration includes email subject, email body, days back to search, and test mode.

15-day free trial – unlimited tasks

Documentation

Click "Run task", then enter a product ID (i.e. 1234567890), and Mechanic will email everyone with a paid order on file for that product.

Optionally, add a space followed by a numeric price to filter for only orders that have exactly that total price. For example, enter 1234567890 100.00 to email customers who ordered product 1234567890, but only if their order total was exactly $100.

Configuration

Use FIRST_NAME and ORDER_NUMBER in the email body to greet the customer by name, with their order number.

Configure a number of days back to search to control how far back you'd like Mechanic to look. (Note: Out of the box, Mechanic can only read the last 60 orders on file for your store. Learn how to raise that limit.)

Enable "Test mode" (don't forget to save!) to have Mechanic show you what emails it would send, if test mode were disabled.

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 some text is submitted (mechanic/user/text)
Options
email subject (required), email body (required, multiline), days back to search (number), test mode (boolean)
Script
{% if event.preview %}
  {
    "action": {
      "type": "email",
      "options": {
        "to": "customer@example.com",
        "subject": {{ options.email_subject__required | strip | json }},
        "body": {{ options.email_body__required_multiline | replace: "FIRST_NAME", "John" | replace: "ORDER_NUMBER", "12345" | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    }
  }
{% else %}
  {% assign arguments = event.data | split: " " %}
  {% assign target_product_id = arguments[0] | plus: 0 %}
  {% assign target_order_total_price = nil %}
  {% if arguments.size > 1 %}
    {% assign target_order_total_price = arguments[1] %}
  {% endif %}

  {% if options.days_back_to_search__number != blank %}
    {% assign days_back_s = options.days_back_to_search__number | times: 24 | times: 60 | times: 60 %}
    {% assign created_at_min_s = "now" | date: "%s" | minus: days_back_s %}
  {% endif %}

  {% for order in shop.orders.any.paid %}
    {% if order.email == blank %}
      {% continue %}
    {% endif %}

    {% if options.days_back_to_search__number != blank %}
      {% assign order_created_at_s = order.created_at | date: "%s" | times: 1 %}
      {% if order_created_at_s < created_at_min_s %}
        {% continue %}
      {% endif %}
    {% endif %}

    {% assign product_ids = order.line_items | map: "product_id" %}
    {% unless product_ids contains target_product_id %}
      {% continue %}
    {% endunless %}

    {% if target_order_total_price != nil and order.total_price != target_order_total_price %}
      {% continue %}
    {% endif %}

    {
      "action": {
        "type": "{% if options.test_mode__boolean %}echo{% else %}email{% endif %}",
        "options": {
          "to": {{ order.email | json }},
          "subject": {{ options.email_subject__required | strip | json }},
          "body": {{ options.email_body__required_multiline | replace: "FIRST_NAME", order.customer.first_name | replace: "ORDER_NUMBER", order.order_number | strip | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      }
    }
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.