Auto-invite customers after an order
Automatically prompt customers to activate their customer accounts, after placing an order in your store, by triggering a customizable Shopify-powered email. Useful if your online store unlocks special offers, functionality, or content after making a purchase. Optionally, only send invitations if the customer has a certain tag and/or has ordered a product with a specific tag.
Try this taskWhen it runs
Occurs whenever an order is created
What you choose
- Only invite if the customer has not yet been invited
- Only invite if the customer has this tag
- Only invite if the order contains a product with this tag
Show 3 more
- Custom invitation email subject
- Custom invitation email message
- Invitation email BCC
Documentation
Automatically prompt customers to activate their customer accounts, after placing an order in your store, by triggering a customizable Shopify-powered email. Useful if your online store unlocks special offers, functionality, or content after making a purchase. Optionally, only send invitations if the customer has a certain tag and/or has ordered a product with a specific tag.
This task works by asking Shopify to send along an invitation email, using the subject and body that you configure here. The email will use your Shopify account's "Customer account invite" email template, available in the "Notifications" area of your Shopify settings.
Note: Because this task triggers a Shopify-powered email, and because this email already uses a Shopify template, the actual message body is optional. (If provided, HTML and CSS are not supported.) And, there's no need to add in an invitation link yourself – this will be taken care of by the Shopify email template as well.
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
shopify/orders/create
How task subscriptions work
Option types and flags
- Only invite if the customer has not yet been invited (boolean)
- Custom invitation email message (multiline)
- Invitation email BCC (array)
Task code
{% assign avoid_duplicate_invites = options.only_invite_if_the_customer_has_not_yet_been_invited__boolean %}
{% assign invite_customer_tag = options.only_invite_if_the_customer_has_this_tag %}
{% assign invite_product_tag = options.only_invite_if_the_order_contains_a_product_with_this_tag %}
{% assign custom_invitation_email_subject = options.custom_invitation_email_subject %}
{% assign custom_invitation_email_message = options.custom_invitation_email_message__multiline %}
{% assign invitation_email_bcc = options.invitation_email_bcc__array %}
{% comment %}
-- get customer and product details from order
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
customer {
id
displayName
defaultEmailAddress {
emailAddress
}
state
tags
}
lineItems(first: 250) {
nodes {
title
product {
tags
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#1185",
"customer": {
"id": "gid://shopify/Customer/1234567890",
"state": "DISABLED",
"tags": {{ invite_customer_tag | json }}
},
"lineItems": {
"nodes": [
{
"product": {
"tags": {{ invite_product_tag | json }}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% assign customer = order.customer %}
{% comment %}
-- verify whether the customer qualifies for an invite based on configured options
{% endcomment %}
{% assign customer_qualifies = true %}
{% if customer == blank or customer.state == "ENABLED" %}
{% assign customer_qualifies = false %}
{% elsif avoid_duplicate_invites and customer.state == "INVITED" %}
{% assign customer_qualifies = false %}
{% elsif invite_customer_tag != blank %}
{% assign customer_tags
= customer.tags
| json
| downcase
| parse_json
%}
{% assign tag_to_match = invite_customer_tag | strip | downcase %}
{% unless customer_tags contains tag_to_match %}
{% assign customer_qualifies = false %}
{% endunless %}
{% endif %}
{% assign order_qualifies = false %}
{% if customer_qualifies %}
{% if invite_product_tag == blank %}
{% assign order_qualifies = true %}
{% else %}
{% for line_item in order.lineItems.nodes %}
{% assign product_tags
= line_item.product.tags
| json
| downcase
| parse_json
%}
{% assign tag_to_match = invite_product_tag | strip | downcase %}
{% if product_tags contains tag_to_match %}
{% assign order_qualifies = true %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% if customer == blank %}
{% log message: "Order does not have a customer; skipping invitation" %}
{% elsif customer_qualifies == false %}
{% log
message: "Customer does not qualify for an invitation",
customer_name: customer.displayName,
customer_state: customer.state,
customer_tags: customer.tags
%}
{% elsif order_qualifies == false and invite_product_tag != blank %}
{% log
message: "No qualifying product found",
invite_product_tag: invite_product_tag,
order: order
%}
{% endif %}
{% if order_qualifies %}
{% action "shopify" %}
mutation {
customerSendAccountInviteEmail(
customerId: {{ customer.id | json }}
email: {
{% if custom_invitation_email_subject != blank %}
subject: {{ custom_invitation_email_subject | json }}
{% endif %}
{% if custom_invitation_email_message != blank %}
customMessage: {{ custom_invitation_email_message | json }}
{% endif %}
{% if invitation_email_bcc != blank %}
bcc: {{ invitation_email_bcc | json }}
{% endif %}
}
) {
customer {
id
displayName
defaultEmailAddress {
emailAddress
}
state
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
Mechanic Liquid reference
Default option values
- Only invite if the customer has not yet been invited
false