forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandardize-uk-shipping-postcodes.json
23 lines (23 loc) · 5.25 KB
/
standardize-uk-shipping-postcodes.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "This task watches for incoming orders for the United Kingdom, and reformats any address postcodes that are not in the standard formats \"XX XXX\", \"XXX XXX\", and \"XXXX XXX\".\r\n\r\nRun this task manually to scan _all_ orders in your store's records. Mechanic will update address postcodes for United Kingdom orders, as described above. If you have a large number of orders in your store, this may take some time.",
"halt_action_run_sequence_on_error": false,
"name": "Standardize UK postcodes",
"online_store_javascript": null,
"options": {
"standardize_billing_postcodes__boolean": true,
"standardize_shipping_postcodes__boolean": true,
"only_scan_open_orders_when_running_manually__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.standardize_billing_postcodes__boolean == false and options.standardize_shipping_postcodes__boolean == false %}\n {\"error\": \"Choose at least one type of postcode to standardize. :)\"}\n{% endif %}\n\n{% assign orders = array %}\n{% assign alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890\" | split: \"\" %}\n\n{% if event.preview %}\n {% assign order = '{\"id\": 1234567890}' | parse_json %}\n\n {% if options.standardize_billing_postcodes__boolean %}\n {% assign order[\"billing_address\"] = '{\"country_code\":\"GB\",\"zip\":\"aa 999AA!\"}' | parse_json %}\n {% endif %}\n\n {% if options.standardize_shipping_postcodes__boolean %}\n {% assign order[\"shipping_address\"] = '{\"country_code\":\"GB\",\"zip\":\"aa 999AA!\"}' | parse_json %}\n {% endif %}\n\n {% assign orders[0] = order %}\n{% elsif event.topic contains \"shopify/orders/\" %}\n {% assign orders[0] = order %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% if options.only_scan_open_orders_when_running_manually__boolean %}\n {% assign orders = shop.orders.any.open %}\n {% else %}\n {% assign orders = shop.orders.any %}\n {% endif %}\n{% endif %}\n\n{% for order in orders %}\n {% assign updates = array %}\n\n {% if options.standardize_billing_postcodes__boolean and order.billing_address.country_code == \"GB\" %}\n {% assign acceptable_characters = array %}\n {% assign characters = order.billing_address.zip | upcase | split: \"\" %}\n {% for character in characters %}\n {% if alphabet contains character %}\n {% assign acceptable_characters[acceptable_characters.size] = character %}\n {% endif %}\n {% endfor %}\n\n {% assign zip_without_space = acceptable_characters | join: \"\" %}\n {% assign length = zip_without_space.size %}\n {% assign part_2_start = zip_without_space.size | minus: 3 %}\n {% assign part_2 = zip_without_space | slice: part_2_start, 3 %}\n {% assign part_1_size = zip_without_space.size | minus: part_2.size %}\n {% assign part_1 = zip_without_space | slice: 0, part_1_size %}\n {% assign zip_standardized = part_1 | append: \" \" | append: part_2 %}\n\n {% if zip_standardized != order.billing_address.zip %}\n {\"log\": {{ \"Standardizing billing address on order \" | append: order.name | append: \" by changing \" | append: order.billing_address.zip | append: \" to \" | append: zip_standardized | json }}}\n {% capture update %}\n \"billing_address\": {\n \"zip\": {{ zip_standardized | json }}\n }\n {% endcapture %}\n {% assign updates[updates.size] = update %}\n {% endif %}\n {% endif %}\n\n {% if options.standardize_shipping_postcodes__boolean and order.shipping_address.country_code == \"GB\" %}\n {% assign acceptable_characters = array %}\n {% assign characters = order.shipping_address.zip | upcase | split: \"\" %}\n {% for character in characters %}\n {% if alphabet contains character %}\n {% assign acceptable_characters[acceptable_characters.size] = character %}\n {% endif %}\n {% endfor %}\n\n {% assign zip_without_space = acceptable_characters | join: \"\" %}\n {% assign length = zip_without_space.size %}\n {% assign part_2_start = zip_without_space.size | minus: 3 %}\n {% assign part_2 = zip_without_space | slice: part_2_start, 3 %}\n {% assign part_1_size = zip_without_space.size | minus: part_2.size %}\n {% assign part_1 = zip_without_space | slice: 0, part_1_size %}\n {% assign zip_standardized = part_1 | append: \" \" | append: part_2 %}\n\n {% if zip_standardized != order.shipping_address.zip %}\n {\"log\": {{ \"Standardizing shipping address on order \" | append: order.name | append: \" by changing \" | append: order.shipping_address.zip | append: \" to \" | append: zip_standardized | json }}}\n {% capture update %}\n \"shipping_address\": {\n \"zip\": {{ zip_standardized | json }}\n }\n {% endcapture %}\n {% assign updates[updates.size] = update %}\n {% endif %}\n {% endif %}\n\n {% if updates != empty %}\n {% action \"shopify\" %}\n [\n \"update\",\n [\"order\", {{ order.id | json }}],\n {\n {{ updates | join: \",\" }}\n }\n ]\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger",
"tags": [
"Shipping",
"UK"
]
}