Mechanic Get Mechanic
Task library

Auto-tag orders by shipping address country

This task automatically adds the tag of your choice to incoming orders, based on the country in the order's shipping address.

Try this task

When it runs

Occurs whenever an order is created

What you choose

Tags to apply and country codes

Documentation

This task automatically adds the tag of your choice to incoming orders, based on the country in the order's shipping address.

Begin by clicking the Add item link next to the Tags to apply and country codes field. Enter tags on the left (one per row), and for each tag add one or more two-character country codes on the right, separated by commas. Country codes should be entered in all caps. Find country codes here.

Example input: Central America > BZ,CR,SV,GT,HN,NI,PA

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
How task subscriptions work

Option types and flags

Task code

{% assign tags_to_apply_and_country_codes = options.tags_to_apply_and_country_codes__keyval_required %}    

{% if event.preview %}
  {% assign order = '{"admin_graphql_api_id": "gid://shopify/Order/1234567890"}' | parse_json %}
  {% assign order["shipping_address"] = hash %}
  {% assign order["shipping_address"]["country_code"] = tags_to_apply_and_country_codes.first.last %}
{% endif %}

{% for tag_to_apply_and_country_codes in tags_to_apply_and_country_codes %}
  {% if tag_to_apply_and_country_codes.last contains order.shipping_address.country_code %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ tag_to_apply_and_country_codes.first | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

    {% break %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference