Mechanic Get Mechanic
Task library

Send account invites to all customers in bulk

Use this task to send out account invitations to all the customers you already have on file, with the push of a button. Optionally, filter by customer tag.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Only invite customers with this tag
  • Invitation email subject
  • Invitation email body

Documentation

Use this task to send out account invitations to all the customers you already have on file, with the push of a button. Optionally, filter by customer tag.

Note: Account invitations are only intended to work with classic customer accounts.

This task sends the same Shopify-powered emails that are used when sending individual customer invitations, and uses the same Shopify email template. A custom subject and message are not required, but will be included when specified.

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

mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task code

{% assign only_invite_customers_with_this_tag = options.only_invite_customers_with_this_tag %}
{% assign invitation_email_subject = options.invitation_email_subject %}
{% assign invitation_email_body = options.invitation_email_body__multiline %}

{% if event.topic == "mechanic/user/trigger" %}
  {% assign search_query = "state:disabled" %}

  {% if only_invite_customers_with_this_tag != blank %}
    {%- capture search_query -%}
      {{ search_query }} AND tag:{{ only_invite_customers_with_this_tag | json }}
    {%- endcapture -%}
  {% endif %}

  {% capture bulk_operation_query %}
    query {
      customers(
        query: {{ search_query | json }}
      ) {
        edges {
          node {
            __typename
            id
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture jsonl_string %}
      {"__typename":"Customer","id":"gid://shopify/Customer/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign customers = bulkOperation.objects | where: "__typename", "Customer" %}

  {% for customer in customers %}
    {% action "shopify" %}
      mutation {
        customerSendAccountInviteEmail(
          customerId: {{ customer.id | json }}
          email: {
            subject: {{ invitation_email_subject | json }}
            customMessage: {{ invitation_email_body | json }}
          }
        ) {
          customer {
            id
            displayName
            defaultEmailAddress {
              emailAddress
            }
            state
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference