Skip to content

Commit

Permalink
fix for returnLineItems field change (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Jul 19, 2024
1 parent 6d58561 commit 5e21d24
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
28 changes: 20 additions & 8 deletions docs/auto-approve-return-requests/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
}
returnLineItems(first: 100) {
nodes {
fulfillmentLineItem {
lineItem {
product {
category {
name
... on ReturnLineItem {
fulfillmentLineItem {
lineItem {
product {
category {
name
}
productType
tags
}
productType
tags
}
}
}
Expand Down Expand Up @@ -78,8 +80,18 @@

{% assign return = result.data.return %}
{% assign order = return.order %}
{% assign return_line_items = return.returnLineItems.nodes %}

{% if return_line_items == blank %}
{% log
message: "No verified return line items were found on this return.",
return: result.data.return
%}
{% break %}
{% endif %}

{% assign return_products
= return.returnLineItems.nodes
= return_line_items
| map: "fulfillmentLineItem"
| map: "lineItem"
| map: "product"
Expand Down
36 changes: 24 additions & 12 deletions docs/send-email-notification-when-items-are-returned/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
name
returnLineItems(first: 100) {
nodes {
quantity
returnReason
fulfillmentLineItem {
lineItem {
name
sku
... on ReturnLineItem {
quantity
returnReason
fulfillmentLineItem {
lineItem {
name
sku
}
}
}
}
}
}
}
}
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
Expand All @@ -47,16 +49,26 @@
}
}
{% endcapture %}

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


{% assign return_line_items = result.data.return.returnLineItems.nodes %}

{% if return_line_items == blank %}
{% log
message: "No verified return line items were found on this return.",
return: result.data.return
%}
{% break %}
{% endif %}

{% capture email_subject %}ALERT: New return {{ result.data.return.name }}{% endcapture %}

{% capture email_body -%}
Returned items
==============
{% for return_line_item in result.data.return.returnLineItems.nodes %}
{% for return_line_item in return_line_items %}
Title: <strong>{{ return_line_item.fulfillmentLineItem.lineItem.name }}</strong>
SKU: <strong>{{ return_line_item.fulfillmentLineItem.lineItem.sku }}</strong>
Quantity: <strong>{{ return_line_item.quantity }}</strong>
Expand Down
2 changes: 1 addition & 1 deletion tasks/auto-approve-return-requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
}
}
],
"script": "{% assign limit_order_tags = options.limit_to_orders_with_any_of_these_tags__array %}\n{% assign limit_product_tags = options.limit_to_products_with_any_of_these_tags__array %}\n{% assign limit_product_categories_or_types = options.limit_to_these_product_categories_or_types__array %}\n{% assign notification_email_recipients = options.notification_email_recipients__array %}\n\n{% if event.topic == \"shopify/returns/request\" %}\n {% comment %}\n -- get additional return and product data not available in the webhook\n {% endcomment %}\n\n {% capture query %}\n query {\n return(id: {{ return.admin_graphql_api_id | json }}) {\n id\n name\n status\n order {\n name\n tags\n }\n returnLineItems(first: 100) {\n nodes {\n fulfillmentLineItem {\n lineItem {\n product {\n category {\n name\n }\n productType\n tags\n }\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 \"return\": {\n \"id\": \"gid://shopify/Return/1234567890\",\n \"name\": \"#PREVIEW-R1\",\n \"status\": \"REQUESTED\",\n \"order\": {\n \"name\": \"#PREVIEW\",\n \"tags\": {{ limit_order_tags.first | json }}\n },\n \"returnLineItems\": {\n \"nodes\": [\n {\n \"fulfillmentLineItem\": {\n \"lineItem\": {\n \"product\": {\n \"category\": {\n \"name\": {{ limit_product_categories_or_types.first | json }}\n },\n \"productType\": {{ limit_product_categories_or_types.first | json }},\n \"tags\": {{ limit_product_tags.first | json }}\n }\n }\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign return = result.data.return %}\n {% assign order = return.order %}\n {% assign return_products\n = return.returnLineItems.nodes\n | map: \"fulfillmentLineItem\"\n | map: \"lineItem\"\n | map: \"product\"\n %}\n\n {% if return.status != \"REQUESTED\" %}\n {% log \"This return request does not have a status of 'REQUESTED' and likely has already been acted on; skipping.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- assume the return request will be approved unless it doesn't meet any configured tag, category, or type limits\n {% endcomment %}\n\n {% assign return_qualifies = true %}\n\n {% if limit_order_tags != blank %}\n {% comment %}\n -- make sure the order has one of the configured tags\n {% endcomment %}\n\n {% assign has_qualifying_order_tag = nil %}\n\n {% for limit_order_tag in limit_order_tags %}\n {% if order.tags contains limit_order_tag %}\n {% assign has_qualifying_order_tag = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless has_qualifying_order_tag %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if limit_product_tags != blank %}\n {% comment %}\n -- make sure each returned product has one of the configured tags\n {% endcomment %}\n\n {% assign all_products_qualify = true %}\n\n {% for product in return_products %}\n {% assign product_qualifies = nil %}\n\n {% for limit_product_tag in limit_product_tags %}\n {% if product.tags contains limit_product_tag %}\n {% assign product_qualifies = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless product_qualifies %}\n {% assign all_products_qualify = false %}\n {% break %}\n {% endunless %}\n {% endfor %}\n\n {% unless all_products_qualify %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if limit_product_categories_or_types != blank %}\n {% comment %}\n -- make sure each returned product is one of the configured categories or types\n {% endcomment %}\n\n {% assign all_products_qualify = true %}\n\n {% for product in return_products %}\n {% assign product_qualifies = nil %}\n\n {% for limit_product_category_or_type in limit_product_categories_or_types %}\n {% if product.productType == limit_product_category_or_type or product.category.name == limit_product_category_or_type %}\n {% assign product_qualifies = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless product_qualifies %}\n {% assign all_products_qualify = false %}\n {% break %}\n {% endunless %}\n {% endfor %}\n\n {% unless all_products_qualify %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if return_qualifies %}\n {% action \"shopify\" %}\n mutation {\n returnApproveRequest(input: {id: {{ return.id | json }}}) {\n return {\n id\n name\n status\n order {\n legacyResourceId\n customer {\n displayName\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/actions/perform\" %}\n {% unless action.type == \"shopify\" and action.run.ok and notification_email_recipients != blank %}\n {% break %}\n {% endunless %}\n\n {% comment %}\n -- if any notification recipients are configured, send them an email when a return request is auto-approved\n {% endcomment %}\n\n {% assign return = action.run.result.data.returnApproveRequest.return %}\n\n {%- capture email_subject -%}\n Return request {{ return.name }} was auto-approved\n {%- endcapture -%}\n\n {%- capture email_body -%}\n Return request {{ return.name }}, made by {{ return.order.customer.displayName }}, was auto-approved.\n\n Review the return details and take further action on the <a href=\"{{ shop.admin_url }}orders/{{ return.order.legacyResourceId }}\">order admin page</a>.\n\n Thanks,\n - Mechanic, for {{ shop.name }}\n {%- endcapture -%}\n\n {% action \"email\" %}\n {\n \"to\": {{ notification_email_recipients | 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",
"script": "{% assign limit_order_tags = options.limit_to_orders_with_any_of_these_tags__array %}\n{% assign limit_product_tags = options.limit_to_products_with_any_of_these_tags__array %}\n{% assign limit_product_categories_or_types = options.limit_to_these_product_categories_or_types__array %}\n{% assign notification_email_recipients = options.notification_email_recipients__array %}\n\n{% if event.topic == \"shopify/returns/request\" %}\n {% comment %}\n -- get additional return and product data not available in the webhook\n {% endcomment %}\n\n {% capture query %}\n query {\n return(id: {{ return.admin_graphql_api_id | json }}) {\n id\n name\n status\n order {\n name\n tags\n }\n returnLineItems(first: 100) {\n nodes {\n ... on ReturnLineItem {\n fulfillmentLineItem {\n lineItem {\n product {\n category {\n name\n }\n productType\n tags\n }\n }\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 \"return\": {\n \"id\": \"gid://shopify/Return/1234567890\",\n \"name\": \"#PREVIEW-R1\",\n \"status\": \"REQUESTED\",\n \"order\": {\n \"name\": \"#PREVIEW\",\n \"tags\": {{ limit_order_tags.first | json }}\n },\n \"returnLineItems\": {\n \"nodes\": [\n {\n \"fulfillmentLineItem\": {\n \"lineItem\": {\n \"product\": {\n \"category\": {\n \"name\": {{ limit_product_categories_or_types.first | json }}\n },\n \"productType\": {{ limit_product_categories_or_types.first | json }},\n \"tags\": {{ limit_product_tags.first | json }}\n }\n }\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign return = result.data.return %}\n {% assign order = return.order %}\n {% assign return_line_items = return.returnLineItems.nodes %}\n\n {% if return_line_items == blank %}\n {% log\n message: \"No verified return line items were found on this return.\",\n return: result.data.return\n %}\n {% break %}\n {% endif %}\n\n {% assign return_products\n = return_line_items\n | map: \"fulfillmentLineItem\"\n | map: \"lineItem\"\n | map: \"product\"\n %}\n\n {% if return.status != \"REQUESTED\" %}\n {% log \"This return request does not have a status of 'REQUESTED' and likely has already been acted on; skipping.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- assume the return request will be approved unless it doesn't meet any configured tag, category, or type limits\n {% endcomment %}\n\n {% assign return_qualifies = true %}\n\n {% if limit_order_tags != blank %}\n {% comment %}\n -- make sure the order has one of the configured tags\n {% endcomment %}\n\n {% assign has_qualifying_order_tag = nil %}\n\n {% for limit_order_tag in limit_order_tags %}\n {% if order.tags contains limit_order_tag %}\n {% assign has_qualifying_order_tag = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless has_qualifying_order_tag %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if limit_product_tags != blank %}\n {% comment %}\n -- make sure each returned product has one of the configured tags\n {% endcomment %}\n\n {% assign all_products_qualify = true %}\n\n {% for product in return_products %}\n {% assign product_qualifies = nil %}\n\n {% for limit_product_tag in limit_product_tags %}\n {% if product.tags contains limit_product_tag %}\n {% assign product_qualifies = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless product_qualifies %}\n {% assign all_products_qualify = false %}\n {% break %}\n {% endunless %}\n {% endfor %}\n\n {% unless all_products_qualify %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if limit_product_categories_or_types != blank %}\n {% comment %}\n -- make sure each returned product is one of the configured categories or types\n {% endcomment %}\n\n {% assign all_products_qualify = true %}\n\n {% for product in return_products %}\n {% assign product_qualifies = nil %}\n\n {% for limit_product_category_or_type in limit_product_categories_or_types %}\n {% if product.productType == limit_product_category_or_type or product.category.name == limit_product_category_or_type %}\n {% assign product_qualifies = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless product_qualifies %}\n {% assign all_products_qualify = false %}\n {% break %}\n {% endunless %}\n {% endfor %}\n\n {% unless all_products_qualify %}\n {% assign return_qualifies = false %}\n {% endunless %}\n {% endif %}\n\n {% if return_qualifies %}\n {% action \"shopify\" %}\n mutation {\n returnApproveRequest(input: {id: {{ return.id | json }}}) {\n return {\n id\n name\n status\n order {\n legacyResourceId\n customer {\n displayName\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/actions/perform\" %}\n {% unless action.type == \"shopify\" and action.run.ok and notification_email_recipients != blank %}\n {% break %}\n {% endunless %}\n\n {% comment %}\n -- if any notification recipients are configured, send them an email when a return request is auto-approved\n {% endcomment %}\n\n {% assign return = action.run.result.data.returnApproveRequest.return %}\n\n {%- capture email_subject -%}\n Return request {{ return.name }} was auto-approved\n {%- endcapture -%}\n\n {%- capture email_body -%}\n Return request {{ return.name }}, made by {{ return.order.customer.displayName }}, was auto-approved.\n\n Review the return details and take further action on the <a href=\"{{ shop.admin_url }}orders/{{ return.order.legacyResourceId }}\">order admin page</a>.\n\n Thanks,\n - Mechanic, for {{ shop.name }}\n {%- endcapture -%}\n\n {% action \"email\" %}\n {\n \"to\": {{ notification_email_recipients | 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",
"subscriptions": [
"shopify/returns/request",
"mechanic/actions/perform"
Expand Down
Loading

0 comments on commit 5e21d24

Please sign in to comment.