Auto-tag orders by shipping address country, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever an order is created. Configuration includes tags to apply and country codes.

15-day free trial – unlimited tasks

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

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tags to apply and country codes (keyval, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more