Standardize UK shipping postcodes with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Standardize UK shipping postcodes

by Isaac Bowen (team@usemechanic.com)

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

Runs when an order is created and when a user triggers the task. Configuration includes standardize billing postcodes, standardize shipping postcodes, and only scan open orders when running manually.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
Options
standardize billing postcodes (boolean), standardize shipping postcodes (boolean), only scan open orders when running manually (boolean)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Standardize billing postcodes
true
Standardize shipping postcodes
true
Only scan open orders when running manually
true