Skip to content
Open
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
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Auto-tag orders when paid](./auto-tag-orders-when-paid)
* [Auto-tag orders with UTM parameters](./auto-tag-orders-with-utm-parameters)
* [Auto-tag orders with a cart attribute](./auto-tag-orders-with-a-cart-attribute)
* [Auto-tag orders with a post office box](./auto-tag-orders-with-a-post-office-box)
* [Auto-tag orders with chargeback or inquiry activity](./auto-tag-orders-with-chargeback-or-inquiry-activity)
* [Auto-tag orders with customer tags when new customer tags are added](./tag-orders-with-customer-tags-when-customer-is-tagged)
* [Auto-tag orders with mismatching billing and shipping addresses](./auto-tag-orders-with-mismatching-billing-and-shipping-addresses)
Expand Down Expand Up @@ -379,6 +380,7 @@ This directory is built automatically. Each task's documentation is generated fr

### Address

* [Auto-tag orders with a post office box](./auto-tag-orders-with-a-post-office-box)
* [Auto-tag orders with mismatching billing and shipping addresses](./auto-tag-orders-with-mismatching-billing-and-shipping-addresses)
* [Email vendors when an order shipping address changes](./email-vendors-when-an-order-shipping-address-changes)
* [Update empty customer data from addresses](./update-empty-customer-data-from-addresses)
Expand Down Expand Up @@ -495,6 +497,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Auto-tag orders when paid](./auto-tag-orders-when-paid)
* [Auto-tag orders with UTM parameters](./auto-tag-orders-with-utm-parameters)
* [Auto-tag orders with a cart attribute](./auto-tag-orders-with-a-cart-attribute)
* [Auto-tag orders with a post office box](./auto-tag-orders-with-a-post-office-box)
* [Auto-tag orders with chargeback or inquiry activity](./auto-tag-orders-with-chargeback-or-inquiry-activity)
* [Auto-tag orders with customer tags when new customer tags are added](./tag-orders-with-customer-tags-when-customer-is-tagged)
* [Auto-tag orders with mismatching billing and shipping addresses](./auto-tag-orders-with-mismatching-billing-and-shipping-addresses)
Expand Down Expand Up @@ -1244,6 +1247,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Auto-tag orders when paid](./auto-tag-orders-when-paid)
* [Auto-tag orders with UTM parameters](./auto-tag-orders-with-utm-parameters)
* [Auto-tag orders with a cart attribute](./auto-tag-orders-with-a-cart-attribute)
* [Auto-tag orders with a post office box](./auto-tag-orders-with-a-post-office-box)
* [Auto-tag orders with chargeback or inquiry activity](./auto-tag-orders-with-chargeback-or-inquiry-activity)
* [Auto-tag orders with customer tags when new customer tags are added](./tag-orders-with-customer-tags-when-customer-is-tagged)
* [Auto-tag orders with product vendors](./auto-tag-orders-with-product-vendors)
Expand Down Expand Up @@ -1608,6 +1612,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Auto-tag orders by shipping address city](./auto-tag-orders-by-shipping-address-city)
* [Auto-tag orders by shipping address country](./auto-tag-orders-by-shipping-address-country)
* [Auto-tag orders that are ready to ship](./auto-tag-orders-that-are-ready-to-ship)
* [Auto-tag orders with a post office box](./auto-tag-orders-with-a-post-office-box)
* [Auto-tag orders with mismatching billing and shipping addresses](./auto-tag-orders-with-mismatching-billing-and-shipping-addresses)
* [Auto-tag orders with their tracking numbers](./auto-tag-orders-with-their-tracking-numbers)
* [Email the customer when tracking numbers are added to their order](./email-the-customer-when-tracking-numbers-are-added-to-their-order)
Expand Down
47 changes: 47 additions & 0 deletions docs/auto-tag-orders-with-a-post-office-box/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Auto-tag orders with a post office box

Tags: Address, Auto-Tag, Orders, Shipping

This task monitors for newly-created orders, and auto-tags any that have a detected post office box in the shipping address. This is useful for shipping services that do not service postal boxes. The task can also optionally send an email notification when a P.O. Box is detected.

* View in the task library: [tasks.mechanic.dev/auto-tag-orders-with-a-post-office-box](https://tasks.mechanic.dev/auto-tag-orders-with-a-post-office-box)
* Task JSON, for direct import: [task.json](../../tasks/auto-tag-orders-with-a-post-office-box.json)
* Preview task code: [script.liquid](./script.liquid)

## Default options

```json
{
"tag_to_add__required": "PO-Box",
"send_email_notification__boolean": false,
"email_recipient__email": "",
"email_subject": "P.O. Box Detected - Order {{ order.name }}",
"email_body__multiline": "Order {{ order.name }} was flagged as having a P.O. Box in the shipping address:\n\nName: {{ order.shipping_address.first_name }} {{ order.shipping_address.last_name }}\nCompany: {{ order.shipping_address.company }}\nPhone: {{ order.shipping_address.phone }}\n\nShipping Address:\n{{ order.shipping_address.address1 }}\n{{ order.shipping_address.address2 }}\n{{ order.shipping_address.city }}, {{ order.shipping_address.province_code }} {{ order.shipping_address.zip }}\n{{ order.shipping_address.country }}\n\n\nThanks,\n{{ shop.name }}"
}
```

[Learn about task options in Mechanic](https://learn.mechanic.dev/core/tasks/options)

## Subscriptions

```liquid
shopify/orders/create
```

[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)

## Documentation

This task monitors for newly-created orders, and auto-tags any that have a detected post office box in the shipping address. This is useful for shipping services that do not service postal boxes. The task can also optionally send an email notification when a P.O. Box is detected.

## Installing this task

Find this task [in the library at tasks.mechanic.dev](https://tasks.mechanic.dev/auto-tag-orders-with-a-post-office-box), and use the "Try this task" button. Or, import [this task's JSON export](../../tasks/auto-tag-orders-with-a-post-office-box.json) – see [Importing and exporting tasks](https://learn.mechanic.dev/core/tasks/import-and-export) to learn how imports work.

## Contributions

Found a bug? Got an improvement to add? Start here: [../../CONTRIBUTING.md](../../CONTRIBUTING.md).

## Task requests

Submit your [task requests](https://mechanic.canny.io/task-requests) for consideration by the Mechanic community, and they may be chosen for development and inclusion in the [task library](https://tasks.mechanic.dev/)!
89 changes: 89 additions & 0 deletions docs/auto-tag-orders-with-a-post-office-box/script.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{% assign tag_to_add = options.tag_to_add__required %}
{% assign send_email_notification = options.send_email_notification__boolean %}
{% assign email_recipient = options.email_recipient__email %}
{% assign email_subject = options.email_subject %}
{% assign email_body = options.email_body__multiline %}

{% comment %}
Check address lines 1 and 2 for a post office box
The matching logic strips out all spaces and special characters,
then matches against one of several strings followed by a digit
{% endcomment %}

{% assign order_has_po_box = false %}
{% assign po_box_regex = "(POB|POBOX|POSTBOX|POSTALBOX|POSTOFFICEBOX|BOXNUMBER|BOXNO|^BOX)\d+" %}

{% assign po_box_match1
= order.shipping_address.address1
| scan: "[A-Za-z0-9]+"
| join: ""
| upcase
| match: po_box_regex
%}
{% assign po_box_match2
= order.shipping_address.address2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe address2 field can be null in the webhook (at least per the Shopify dev docs). If so, then the scan will throw an error as it expects a string.

Just pop an if statement around this I think.

| scan: "[A-Za-z0-9]+"
| join: ""
| upcase
| match: po_box_regex
%}

{% if po_box_match1 or po_box_match2 %}
{% assign order_has_po_box = true %}
{% endif %}

{% comment %}
IF PO Box is detected, add tag to order
{% endcomment %}
{% if order_has_po_box %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.admin_graphql_api_id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}

{% if send_email_notification %}

{% comment %}
Show error if emails are enabled and any of the fields are missing
{% endcomment %}
{% if email_recipient == blank or email_subject == blank or email_body == blank %}
{% assign error_message = "Email Notifications are enabled, please add:" %}
{% assign error_topics = array %}
{% if email_recipient == blank %}
{% assign error_topics = error_topics | push: "- a valid email recipient" %}
{% endif %}
{% if email_subject == blank %}
{% assign error_topics = error_topics | push: "- an email subject" %}
{% endif %}
{% if email_body == blank %}
{% assign error_topics = error_topics | push: "- an email body" %}
{% endif %}
{% capture error_message %}
{{- error_message }}{{ newline }}
{{- error_topics | join: newline }}
{%- endcapture %}
{% error error_message %}
{% endif %}

{% action "email" %}
{
"to": {{ email_recipient | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% else %}
{% log "No PO Box detected" %}
{% endif %}
62 changes: 62 additions & 0 deletions tasks/auto-tag-orders-with-a-post-office-box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"docs": "This task monitors for newly-created orders, and auto-tags any that have a detected post office box in the shipping address. This is useful for shipping services that do not service postal boxes. The task can also optionally send an email notification when a P.O. Box is detected.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders with a post office box",
"online_store_javascript": null,
"options": {
"tag_to_add__required": "PO-Box",
"send_email_notification__boolean": false,
"email_recipient__email": "",
"email_subject": "P.O. Box Detected - Order {{ order.name }}",
"email_body__multiline": "Order {{ order.name }} was flagged as having a P.O. Box in the shipping address:\n\nName: {{ order.shipping_address.first_name }} {{ order.shipping_address.last_name }}\nCompany: {{ order.shipping_address.company }}\nPhone: {{ order.shipping_address.phone }}\n\nShipping Address:\n{{ order.shipping_address.address1 }}\n{{ order.shipping_address.address2 }}\n{{ order.shipping_address.city }}, {{ order.shipping_address.province_code }} {{ order.shipping_address.zip }}\n{{ order.shipping_address.country }}\n\n\nThanks,\n{{ shop.name }}"
},
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [
{
"description": "An order with a PO Box",
"event_attributes": {
"topic": "shopify/orders/create",
"data": {
"admin_graphql_api_id": "gid://shopify/Order/1234567890",
"name": "#1030",
"shipping_address": {
"address1": "123 Main Street",
"address2": "PO Box 243",
"city": "Springfield",
"company": "Widgets Inc",
"country": "United States",
"first_name": "Marge",
"last_name": "Simpson",
"phone": "+1-855-553-4763",
"province_code": "IL",
"zip": "62701"
}
}
}
},
{
"description": "An order without a PO Box",
"event_attributes": {
"topic": "shopify/orders/create",
"data": {
"admin_graphql_api_id": "gid://shopify/Order/1234567890",
"shipping_address": {
"address1": "123 Main Street",
"address2": ""
}
}
}
}
],
"script": "{% assign tag_to_add = options.tag_to_add__required %}\n{% assign send_email_notification = options.send_email_notification__boolean %}\n{% assign email_recipient = options.email_recipient__email %}\n{% assign email_subject = options.email_subject %}\n{% assign email_body = options.email_body__multiline %}\n\n{% comment %} \n Check address lines 1 and 2 for a post office box\n The matching logic strips out all spaces and special characters,\n then matches against one of several strings followed by a digit\n{% endcomment %}\n\n{% assign order_has_po_box = false %}\n{% assign po_box_regex = \"(POB|POBOX|POSTBOX|POSTALBOX|POSTOFFICEBOX|BOXNUMBER|BOXNO|^BOX)\\d+\" %}\n\n{% assign po_box_match1\n = order.shipping_address.address1\n | scan: \"[A-Za-z0-9]+\"\n | join: \"\"\n | upcase\n | match: po_box_regex\n%}\n{% assign po_box_match2\n = order.shipping_address.address2\n | scan: \"[A-Za-z0-9]+\"\n | join: \"\"\n | upcase\n | match: po_box_regex\n%}\n\n{% if po_box_match1 or po_box_match2 %}\n {% assign order_has_po_box = true %}\n{% endif %}\n\n{% comment %}\n IF PO Box is detected, add tag to order\n{% endcomment %}\n{% if order_has_po_box %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.admin_graphql_api_id | json }}\n tags: {{ tag_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% if send_email_notification %}\n\n {% comment %}\n Show error if emails are enabled and any of the fields are missing\n {% endcomment %}\n {% if email_recipient == blank or email_subject == blank or email_body == blank %}\n {% assign error_message = \"Email Notifications are enabled, please add:\" %}\n {% assign error_topics = array %}\n {% if email_recipient == blank %}\n {% assign error_topics = error_topics | push: \"- a valid email recipient\" %}\n {% endif %}\n {% if email_subject == blank %}\n {% assign error_topics = error_topics | push: \"- an email subject\" %}\n {% endif %}\n {% if email_body == blank %}\n {% assign error_topics = error_topics | push: \"- an email body\" %}\n {% endif %}\n {% capture error_message %}\n {{- error_message }}{{ newline }}\n {{- error_topics | join: newline }}\n {%- endcapture %}\n {% error error_message %}\n {% endif %}\n\n {% action \"email\" %}\n {\n \"to\": {{ email_recipient | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | newline_to_br | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }}\n }\n {% endaction %}\n {% endif %}\n{% else %}\n {% log \"No PO Box detected\" %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create"
],
"subscriptions_template": "shopify/orders/create",
"tags": [
"Address",
"Auto-Tag",
"Orders",
"Shipping"
]
}