Mechanic Get Mechanic
Task library

Standardize UK postcodes

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".

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs when a user manually triggers the task

What you choose

  • Standardize billing postcodes
  • Standardize shipping postcodes
  • Only scan open orders when running manually

Documentation

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".

Run 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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/create
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% if options.standardize_billing_postcodes__boolean == false and options.standardize_shipping_postcodes__boolean == false %}
  {"error": "Choose at least one type of postcode to standardize. :)"}
{% endif %}

{% assign orders = array %}
{% assign alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890" | split: "" %}

{% if event.preview %}
  {% assign order = '{"id": 1234567890}' | parse_json %}

  {% if options.standardize_billing_postcodes__boolean %}
    {% assign order["billing_address"] = '{"country_code":"GB","zip":"aa 999AA!"}' | parse_json %}
  {% endif %}

  {% if options.standardize_shipping_postcodes__boolean %}
    {% assign order["shipping_address"] = '{"country_code":"GB","zip":"aa 999AA!"}' | parse_json %}
  {% endif %}

  {% assign orders[0] = order %}
{% elsif event.topic contains "shopify/orders/" %}
  {% assign orders[0] = order %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% if options.only_scan_open_orders_when_running_manually__boolean %}
    {% assign orders = shop.orders.any.open %}
  {% else %}
    {% assign orders = shop.orders.any %}
  {% endif %}
{% endif %}

{% for order in orders %}
  {% assign updates = array %}

  {% if options.standardize_billing_postcodes__boolean and order.billing_address.country_code == "GB" %}
    {% assign acceptable_characters = array %}
    {% assign characters = order.billing_address.zip | upcase | split: "" %}
    {% for character in characters %}
      {% if alphabet contains character %}
        {% assign acceptable_characters[acceptable_characters.size] = character %}
      {% endif %}
    {% endfor %}

    {% assign zip_without_space = acceptable_characters | join: "" %}
    {% assign length = zip_without_space.size %}
    {% assign part_2_start = zip_without_space.size | minus: 3 %}
    {% assign part_2 = zip_without_space | slice: part_2_start, 3 %}
    {% assign part_1_size = zip_without_space.size | minus: part_2.size %}
    {% assign part_1 = zip_without_space | slice: 0, part_1_size %}
    {% assign zip_standardized = part_1 | append: " " | append: part_2 %}

    {% if zip_standardized != order.billing_address.zip %}
      {"log": {{ "Standardizing billing address on order " | append: order.name | append: " by changing " | append: order.billing_address.zip | append: " to " | append: zip_standardized | json }}}
      {% capture update %}
        "billing_address": {
          "zip": {{ zip_standardized | json }}
        }
      {% endcapture %}
      {% assign updates[updates.size] = update %}
    {% endif %}
  {% endif %}

  {% if options.standardize_shipping_postcodes__boolean and order.shipping_address.country_code == "GB" %}
    {% assign acceptable_characters = array %}
    {% assign characters = order.shipping_address.zip | upcase | split: "" %}
    {% for character in characters %}
      {% if alphabet contains character %}
        {% assign acceptable_characters[acceptable_characters.size] = character %}
      {% endif %}
    {% endfor %}

    {% assign zip_without_space = acceptable_characters | join: "" %}
    {% assign length = zip_without_space.size %}
    {% assign part_2_start = zip_without_space.size | minus: 3 %}
    {% assign part_2 = zip_without_space | slice: part_2_start, 3 %}
    {% assign part_1_size = zip_without_space.size | minus: part_2.size %}
    {% assign part_1 = zip_without_space | slice: 0, part_1_size %}
    {% assign zip_standardized = part_1 | append: " " | append: part_2 %}

    {% if zip_standardized != order.shipping_address.zip %}
      {"log": {{ "Standardizing shipping address on order " | append: order.name | append: " by changing " | append: order.shipping_address.zip | append: " to " | append: zip_standardized | json }}}
      {% capture update %}
        "shipping_address": {
          "zip": {{ zip_standardized | json }}
        }
      {% endcapture %}
      {% assign updates[updates.size] = update %}
    {% endif %}
  {% endif %}

  {% if updates != empty %}
    {% action "shopify" %}
      [
        "update",
        ["order", {{ order.id | json }}],
        {
          {{ updates | join: "," }}
        }
      ]
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference

Default option values

Standardize billing postcodes
true
Standardize shipping postcodes
true
Only scan open orders when running manually
true