Skip to content

Commit

Permalink
allow filtering by product title
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus committed Nov 13, 2023
1 parent f58348f commit 34c7c6b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
17 changes: 12 additions & 5 deletions docs/send-tax-receipts-for-donations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Tags: Orders, PDF, Receipts, Tax

Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, and it will sum the donation amount from all line items that have a configured product donation tag.
Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, or optionally on order creation. The task will sum the donation amount from all line items that have either one of the configured product donation tags, or one of the configured product titles.

* View in the task library: [tasks.mechanic.dev/send-tax-receipts-for-donations](https://tasks.mechanic.dev/send-tax-receipts-for-donations)
* Task JSON, for direct import: [task.json](../../tasks/send-tax-receipts-for-donations.json)
Expand All @@ -13,9 +13,11 @@ Use this task to email donation receipts to donors as PDF attachments, with an o
```json
{
"tax_id__required": null,
"run_on_order_creation_instead_of_paid__boolean": false,
"identify_donation_products_with_any_of_these_tags__array_required": [
"donation"
],
"identify_donation_products_with_any_of_these_titles__array": null,
"send_cc_to_shop_email__boolean": false,
"email_subject__required": "Receipt {{ order.name }} for donation to {{ shop.name }}",
"email_body__multiline_required": "Thank you for your recent donation!\n\nAttached you will find your official donation receipt.",
Expand All @@ -29,23 +31,28 @@ Use this task to email donation receipts to donors as PDF attachments, with an o
## Subscriptions

```liquid
shopify/orders/paid
{% if options.run_on_order_creation_instead_of_paid__boolean %}
shopify/orders/create
{% else %}
shopify/orders/paid
{% endif %}
mechanic/user/order
```

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

## Documentation

Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, and it will sum the donation amount from all line items that have a configured product donation tag.
Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, or optionally on order creation. The task will sum the donation amount from all line items that have either one of the configured product donation tags, or one of the configured product titles.

This task can also receive orders sent directly via [admin action links](https://learn.mechanic.dev/core/shopify/admin-action-links#link-usage). When doing so, the task will **not** check that the order is paid to allow for flexibility when sending donation receipts manually.
This task can also receive individual orders sent directly via [admin action links](https://learn.mechanic.dev/core/shopify/admin-action-links#link-usage).

**Notes:**

- The task comes with a sample donation tag, email subject, email body, donation receipt filename, and donation receipt HTML template. The HTML template is used by the PDF file generator, so care must be taken to provide valid HTML if this field is customized.
- The *[TAX_ID]* and *[DONATION_AMOUNT]* variables need to be present in the HTML template in order for those values to be included in the PDF donation receipt.
- Orders with no donation products will be ignored.
- The task will identify donation products either by tag or title. Only one of these option fields can be configured for use.
- Orders with no donation products will be ignored by this task.

## Installing this task

Expand Down
45 changes: 32 additions & 13 deletions docs/send-tax-receipts-for-donations/script.liquid
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
{% assign tax_id = options.tax_id__required %}
{% assign donation_product_tags = options.identify_donation_products_with_any_of_these_tags__array_required %}
{% comment %}{{ options.run_on_order_creation_instead_of_paid__boolean }}{% endcomment %}
{% assign donation_product_tags = options.identify_donation_products_with_any_of_these_tags__array %}
{% assign donation_product_titles = options.identify_donation_products_with_any_of_these_titles__array %}
{% assign send_cc_to_shop_email = options.send_cc_to_shop_email__boolean %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}
{% assign donation_receipt_filename = options.donation_receipt_filename__required %}
{% assign donation_reciept_html_template = options.donation_reciept_html_template__code_multiline_required %}

{% if event.topic == "shopify/orders/paid" or event.topic == "mechanic/user/order" %}
{% comment %}
-- NOTE: if this order was sent directly via an admin link, then the "paid" financial status will not be checked to allow for flexibility on manual sends
{% endcomment %}
{% if donation_product_tags != blank and donation_product_titles != blank %}
{% error "Enter either product tags to filter on, or product titles, but not both." %}
{% elsif donation_product_tags == blank and donation_product_titles == blank %}
{% error "You must enter either product tags or titles to filter on." %}
{% endif %}

{% if event.topic == "shopify/orders/paid"
or event.topic == "shopify/orders/create"
or event.topic == "mechanic/user/order"
%}
{% if order.email == blank %}
{% log "This order does not have an email associated with it." %}
{% break %}
{% endif %}

{% comment %}
-- get the order data to see if there are any donation products on this order, by product tag
-- get the order data to see if there are any donation products on this order, by product tag or title
{% endcomment %}

{% capture query %}
Expand All @@ -29,6 +36,7 @@
nodes {
product {
tags
title
}
originalTotalSet {
presentmentMoney {
Expand All @@ -53,7 +61,8 @@
"nodes": [
{
"product": {
"tags": {{ donation_product_tags.first | json }}
"tags": {{ donation_product_tags.first | json }},
"title": {{ donation_product_titles.first | json }}
},
"originalTotalSet": {
"presentmentMoney": {
Expand All @@ -78,12 +87,22 @@
{% assign donation_amount = 0 %}

{% for line_item in result.data.order.lineItems.nodes %}
{% for donation_product_tag in donation_product_tags %}
{% if line_item.product.tags contains donation_product_tag %}
{% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}
{% break %}
{% endif %}
{% endfor %}
{% if donation_product_tags != blank %}
{% for donation_product_tag in donation_product_tags %}
{% if line_item.product.tags contains donation_product_tag %}
{% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}
{% break %}
{% endif %}
{% endfor %}

{% elsif donation_product_titles != blank %}
{% for donation_product_title in donation_product_titles %}
{% if line_item.product.title == donation_product_title %}
{% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}

{% unless donation_amount > 0 %}
Expand Down
8 changes: 5 additions & 3 deletions tasks/send-tax-receipts-for-donations.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"docs": "Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, and it will sum the donation amount from all line items that have a configured product donation tag.\n\nThis task can also receive orders sent directly via [admin action links](https://learn.mechanic.dev/core/shopify/admin-action-links#link-usage). When doing so, the task will **not** check that the order is paid to allow for flexibility when sending donation receipts manually.\n\n**Notes:**\n\n- The task comes with a sample donation tag, email subject, email body, donation receipt filename, and donation receipt HTML template. The HTML template is used by the PDF file generator, so care must be taken to provide valid HTML if this field is customized.\n- The *[TAX_ID]* and *[DONATION_AMOUNT]* variables need to be present in the HTML template in order for those values to be included in the PDF donation receipt.\n- Orders with no donation products will be ignored.",
"docs": "Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, or optionally on order creation. The task will sum the donation amount from all line items that have either one of the configured product donation tags, or one of the configured product titles.\n\nThis task can also receive individual orders sent directly via [admin action links](https://learn.mechanic.dev/core/shopify/admin-action-links#link-usage).\n\n**Notes:**\n\n- The task comes with a sample donation tag, email subject, email body, donation receipt filename, and donation receipt HTML template. The HTML template is used by the PDF file generator, so care must be taken to provide valid HTML if this field is customized.\n- The *[TAX_ID]* and *[DONATION_AMOUNT]* variables need to be present in the HTML template in order for those values to be included in the PDF donation receipt.\n- The task will identify donation products either by tag or title. Only one of these option fields can be configured for use.\n- Orders with no donation products will be ignored by this task.",
"halt_action_run_sequence_on_error": false,
"name": "Send tax receipts for donations",
"online_store_javascript": null,
"options": {
"tax_id__required": null,
"run_on_order_creation_instead_of_paid__boolean": false,
"identify_donation_products_with_any_of_these_tags__array_required": [
"donation"
],
"identify_donation_products_with_any_of_these_titles__array": null,
"send_cc_to_shop_email__boolean": false,
"email_subject__required": "Receipt {{ order.name }} for donation to {{ shop.name }}",
"email_body__multiline_required": "Thank you for your recent donation!\n\nAttached you will find your official donation receipt.",
Expand All @@ -17,12 +19,12 @@
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign tax_id = options.tax_id__required %}\n{% assign donation_product_tags = options.identify_donation_products_with_any_of_these_tags__array_required %}\n{% assign send_cc_to_shop_email = options.send_cc_to_shop_email__boolean %}\n{% assign email_subject = options.email_subject__required %}\n{% assign email_body = options.email_body__multiline_required %}\n{% assign donation_receipt_filename = options.donation_receipt_filename__required %}\n{% assign donation_reciept_html_template = options.donation_reciept_html_template__code_multiline_required %}\n\n{% if event.topic == \"shopify/orders/paid\" or event.topic == \"mechanic/user/order\" %}\n {% comment %}\n -- NOTE: if this order was sent directly via an admin link, then the \"paid\" financial status will not be checked to allow for flexibility on manual sends\n {% endcomment %}\n\n {% if order.email == blank %}\n {% log \"This order does not have an email associated with it.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- get the order data to see if there are any donation products on this order, by product tag\n {% endcomment %}\n\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n presentmentCurrencyCode\n lineItems(first: 100) {\n nodes {\n product {\n tags\n }\n originalTotalSet {\n presentmentMoney {\n amount\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"presentmentCurrencyCode\": \"USD\",\n \"lineItems\": {\n \"nodes\": [\n {\n \"product\": {\n \"tags\": {{ donation_product_tags.first | json }}\n },\n \"originalTotalSet\": {\n \"presentmentMoney\": {\n \"amount\": \"100.0\"\n }\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- for any donation products, add the non-discounted total for that line item to the runnning donation amount\n {% endcomment %}\n\n {% assign donation_amount = 0 %}\n\n {% for line_item in result.data.order.lineItems.nodes %}\n {% for donation_product_tag in donation_product_tags %}\n {% if line_item.product.tags contains donation_product_tag %}\n {% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% unless donation_amount > 0 %}\n {% log \"This order has no donation products on it; skipping\" %}\n {% break %}\n {% endunless %}\n\n {% assign donation_with_currency = donation_amount | currency: result.data.order.presentmentCurrencyCode %}\n\n {% comment %}\n -- replace variables in the HTML template for the configured tax ID and the calculated donation amount\n {% endcomment %}\n\n {% assign donation_html\n = donation_reciept_html_template\n | replace: \"[TAX_ID]\", tax_id\n | replace: \"[DONATION_AMOUNT]\", donation_with_currency\n %}\n\n {% comment %}\n -- generate tax receipt as a PDF and attach to email to donor; optionally cc the shop email address\n {% endcomment %}\n\n {% action \"email\" %}\n {\n \"to\": {{ order.email | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | newline_to_br | json }},\n {% if send_cc_to_shop_email %}\"cc\": {{ shop.customer_email | json }},{% endif %}\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }},\n \"attachments\": {\n {{ donation_receipt_filename | remove: \"#\" | json }}: {\n \"pdf\": {\n \"html\": {{ donation_html | json }},\n \"__force_pdfcrowd\": true\n }\n }\n }\n }\n {% endaction %}\n{% endif %}\n",
"script": "{% assign tax_id = options.tax_id__required %}\n{% comment %}{{ options.run_on_order_creation_instead_of_paid__boolean }}{% endcomment %}\n{% assign donation_product_tags = options.identify_donation_products_with_any_of_these_tags__array %}\n{% assign donation_product_titles = options.identify_donation_products_with_any_of_these_titles__array %}\n{% assign send_cc_to_shop_email = options.send_cc_to_shop_email__boolean %}\n{% assign email_subject = options.email_subject__required %}\n{% assign email_body = options.email_body__multiline_required %}\n{% assign donation_receipt_filename = options.donation_receipt_filename__required %}\n{% assign donation_reciept_html_template = options.donation_reciept_html_template__code_multiline_required %}\n\n{% if donation_product_tags != blank and donation_product_titles != blank %}\n {% error \"Enter either product tags to filter on, or product titles, but not both.\" %}\n{% elsif donation_product_tags == blank and donation_product_titles == blank %}\n {% error \"You must enter either product tags or titles to filter on.\" %}\n{% endif %}\n\n{% if event.topic == \"shopify/orders/paid\"\n or event.topic == \"shopify/orders/create\"\n or event.topic == \"mechanic/user/order\"\n%}\n {% if order.email == blank %}\n {% log \"This order does not have an email associated with it.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- get the order data to see if there are any donation products on this order, by product tag or title\n {% endcomment %}\n\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n presentmentCurrencyCode\n lineItems(first: 100) {\n nodes {\n product {\n tags\n title\n }\n originalTotalSet {\n presentmentMoney {\n amount\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"presentmentCurrencyCode\": \"USD\",\n \"lineItems\": {\n \"nodes\": [\n {\n \"product\": {\n \"tags\": {{ donation_product_tags.first | json }},\n \"title\": {{ donation_product_titles.first | json }}\n },\n \"originalTotalSet\": {\n \"presentmentMoney\": {\n \"amount\": \"100.0\"\n }\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- for any donation products, add the non-discounted total for that line item to the runnning donation amount\n {% endcomment %}\n\n {% assign donation_amount = 0 %}\n\n {% for line_item in result.data.order.lineItems.nodes %}\n {% if donation_product_tags != blank %}\n {% for donation_product_tag in donation_product_tags %}\n {% if line_item.product.tags contains donation_product_tag %}\n {% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% elsif donation_product_titles != blank %}\n {% for donation_product_title in donation_product_titles %}\n {% if line_item.product.title == donation_product_title %}\n {% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% endif %}\n {% endfor %}\n\n {% unless donation_amount > 0 %}\n {% log \"This order has no donation products on it; skipping\" %}\n {% break %}\n {% endunless %}\n\n {% assign donation_with_currency = donation_amount | currency: result.data.order.presentmentCurrencyCode %}\n\n {% comment %}\n -- replace variables in the HTML template for the configured tax ID and the calculated donation amount\n {% endcomment %}\n\n {% assign donation_html\n = donation_reciept_html_template\n | replace: \"[TAX_ID]\", tax_id\n | replace: \"[DONATION_AMOUNT]\", donation_with_currency\n %}\n\n {% comment %}\n -- generate tax receipt as a PDF and attach to email to donor; optionally cc the shop email address\n {% endcomment %}\n\n {% action \"email\" %}\n {\n \"to\": {{ order.email | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | newline_to_br | json }},\n {% if send_cc_to_shop_email %}\"cc\": {{ shop.customer_email | json }},{% endif %}\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }},\n \"attachments\": {\n {{ donation_receipt_filename | remove: \"#\" | json }}: {\n \"pdf\": {\n \"html\": {{ donation_html | json }},\n \"__force_pdfcrowd\": true\n }\n }\n }\n }\n {% endaction %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/paid",
"mechanic/user/order"
],
"subscriptions_template": "shopify/orders/paid\nmechanic/user/order",
"subscriptions_template": "{% if options.run_on_order_creation_instead_of_paid__boolean %}\n shopify/orders/create\n{% else %}\n shopify/orders/paid\n{% endif %}\nmechanic/user/order",
"tags": [
"Orders",
"PDF",
Expand Down

0 comments on commit 34c7c6b

Please sign in to comment.