Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first batch of task fixes #386

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/auto-tag-customers-upon-product-purchase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ mechanic/shopify/bulk_operation

Configure this task with search terms that describe certain products, and this task will apply the tags of your choice to every customer who purchases a matching product or product variant. Run this task manually to tag all customers who have a qualifying order already on file.

This task auto-tags customers who have paid orders on file for products or product variants that match the search query you add.
Sample searches:
* A specific SKU: search product variants for `sku:ABC123`
* All products with a certain tag: search products for `tag:holiday`
* All products with a certain type: search products for `product_type:"Gift Card"`
* A product with a certain title: search products for `title:"Short sleeve t-shirt"`
This task auto-tags customers who have paid orders on file for products or product variants that match the search query you add.

Sample searches:

* A specific SKU: search product variants for `sku:ABC123`
* All products with a certain tag: search products for `tag:holiday`
* All products with a certain type: search products for `product_type:"Gift Card"`
* A product with a certain title: search products for `title:"Short sleeve t-shirt"`

To ensure expected results, use this task with test mode enabled, before disabling test mode.

## Installing this task
Expand Down
7 changes: 2 additions & 5 deletions docs/auto-tag-customers-upon-product-purchase/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,11 @@
{% assign job["reason"] = order_qualifying_resource | append: ", in order " | append: order.name %}
{% assign jobs[jobs.size] = job %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% assign orders_query = "financial_status:paid" %}

{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
customers(
query: "orders_count:>0"
) {
customers {
edges {
node {
__typename
Expand Down
4 changes: 2 additions & 2 deletions docs/email-customers-when-tagged/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ mechanic/user/trigger

Automatically send out an email to your customers when a tag is added to their account. Useful for sending approval emails, welcome emails if the customer was created with a certain tag, or any other emails related to an update to the customer's status. The most common use of this task is to send out emails to customers when their accounts are approved, but you can use this task to email customers any kind of alert you like, triggered by a simple tag. For example, some themes include an email subscription form that creates a customer record, pre-tagged with "email subscribe", or something to that effect.

**Important note:** After saving this task for the first time (and every time you change the tag to watch for), click the "Run task" button. Mechanic will then scan your customers to find those who _already_ have the desired tag, and will make a note of them, so as to not email them in the future. (We require this to avoid suddenly sending a flood of emails to customers that were tagged before the Mechanic task was created.)
**Important note:** After saving this task for the first time (and every time you change the tag to watch for), click the "Run task" button. Mechanic will then scan your customers to find those who _already_ have the desired tag, and will make a note of them, so as to not email them in the future. (We require this to avoid suddenly sending a flood of emails to customers that were tagged before the Mechanic task was created.)

Enable the "Autotag customers after emailing" option to have Mechanic add "YOURTAGHERE-email-sent" to the customer's tags, when their email is sent out.

## Installing this task
Expand Down
110 changes: 51 additions & 59 deletions docs/email-customers-when-tagged/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,24 @@
}
{% endaction %}
{% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
{% assign customer_ids_to_mark = array %}
{% assign metafield_inputs = array %}
{% assign cursor = nil %}

{% for n in (0..100) %}
{% capture query %}
query {
customers(
first: 250
query: {{ approval_tag | json | prepend: "tag:" | json }}
customerSegmentMembers(
first: 1000
query: "customer_tags CONTAINS '{{ approval_tag }}'"
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
metafield(
Expand All @@ -174,14 +175,11 @@
{% capture result_json %}
{
"data": {
"customers": {
"pageInfo": {
"hasNextPage": false
},
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/Customer/1234567890",
"id": "gid://shopify/CustomerSegmentMember/1234567890",
"metafield": null
}
}
Expand All @@ -194,68 +192,62 @@
{% assign result = result_json | parse_json %}
{% endif %}

{% for edge in result.data.customers.edges %}
{% if edge.node.metafield %}
{% continue %}
{% endif %}
{% assign customers = result.data.customerSegmentMembers.edges | map: "node" %}

{% comment %}
-- Note: Shopify API accepts CustomerSegmentMember IDs without needing replacement, at least for the metafieldsSet mutation
{% endcomment %}

{% assign customer_ids_to_mark[customer_ids_to_mark.size] = edge.node.id %}
{% for customer in customers %}
{% if customer.metafield == blank %}
{% assign metafield_input = hash %}
{% assign metafield_input["ownerId"] = customer.id %}
{% assign metafield_input["namespace"] = "mechanic" %}
{% assign metafield_input["key"] = tag_processed_metafield_key %}
{% assign metafield_input["type"] = "number_integer" %}
{% assign metafield_input["value"] = "1" %}
{% assign metafield_inputs = metafield_inputs | push: metafield_input %}
{% endif %}
{% endfor %}

{% if result.data.customers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customers.edges.last.cursor %}
{% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% assign metafields = array %}
{% if metafield_inputs != blank %}
{% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 25, fill_with: false %}

{% for customer_id in customer_ids_to_mark %}
{% capture metafield %}
{
ownerId: {{ customer_id | json }}
namespace: "mechanic"
key: {{ tag_processed_metafield_key | json }}
type: "number_integer"
value: "1"
}
{% endcapture %}

{% assign metafields = metafields | push: metafield %}
{% 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 Customer {
id
{% for group_of_metafield_inputs in groups_of_metafield_inputs %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafield_inputs | graphql_arguments }}
) {
metafields {
id
namespace
key
type
value
owner {
... on Customer {
id
}
}
}
}
userErrors {
code
field
message
userErrors {
code
field
message
}
}
}
}
{% endaction %}
{% endfor %}
{% endaction %}
{% endfor %}
{% endif %}

{% action "shopify" %}
mutation {
Expand Down
4 changes: 3 additions & 1 deletion docs/send-account-invites-to-all-customers-in-bulk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mechanic/shopify/bulk_operation

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.

This task sends the same Shopify-powered emails that are used when sending individual customer invitations ([see Shopify's documentation](https://help.shopify.com/en/manual/customers/customer-accounts#individual-invites)), and uses the same Shopify email template. A custom subject and message are not required, but will be included when specified.
**Note**: Account invitations are only intended to work with [classic customer accounts](https://help.shopify.com/en/manual/customers/customer-accounts/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.

## Installing this task

Expand Down
106 changes: 69 additions & 37 deletions docs/send-account-invites-to-all-customers-in-bulk/script.liquid
Original file line number Diff line number Diff line change
@@ -1,52 +1,84 @@
{% if event.topic == "mechanic/user/trigger" %}
{% assign query = "state:DISABLED" %}
{% assign search_query = "customer_account_status = 'DISABLED'" %}

{% if options.only_invite_customers_with_this_tag %}
{% assign query = query | append: " AND " | append: "tag:" | append: options.only_invite_customers_with_this_tag %}
{%- capture search_query -%}
{{ search_query }} AND customer_tags CONTAINS '{{ options.only_invite_customers_with_this_tag }}'
{%- endcapture -%}
{% endif %}

{% capture bulk_operation_query %}
query {
customers (query: {{ query | json }} ) {
edges {
node {
__typename
legacyResourceId
{% assign cursor = nil %}
{% assign customers = array %}

{% comment %}
-- can query 1000 segment members at a time
{% endcomment %}

{% for n in (1..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
}
{% endcapture %}

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

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/CustomerSegmentMember/1234567890"
}
}
]
}
}
}
}
}
{% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign customers = bulkOperation.objects | where: "__typename", "Customer" %}
{% if event.preview %}
{% assign customers = array %}
{% assign customers[0] = hash %}
{% assign customers[0]["legacyResourceId"] = 12345 %}
{% endif %}
{% for customer in customers %}
{% endcapture %}

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

{% assign customers
= result.data.customerSegmentMembers.edges
| map: "node"
| concat: customers
%}

{% if result.data.customers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% comment %}
-- send invite to all customers with disabled accounts using REST (there is no equivalent GraphQL mutation)
{% endcomment %}

{% for customer in customers %}
{% action "shopify" %}
[
"post",
"/admin/customers/{{ customer.legacyResourceId }}/send_invite.json",
"/admin/customers/{{ customer.id | remove: "gid://shopify/CustomerSegmentMember/" }}/send_invite.json",
{
"customer_invite": {
"subject": {{ options.custom_subject | json }},
Expand Down
Loading
Loading