-
Notifications
You must be signed in to change notification settings - Fork 36
Auto-tag orders with a post office box #522
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
Open
timdmackey
wants to merge
2
commits into
lightward:main
Choose a base branch
from
timdmackey:task/auto-tag-orders-with-a-post-office-box
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/)! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| | 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 %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.