Manage fulfillment status using order tags with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Manage fulfillment status using order tags

by Isaac Bowen (team@usemechanic.com)

Use this task to easily set the shipment status of your single-fulfillment orders. Just add a tag, and a new fulfillment event will be recorded, using the corresponding shipment status.

Runs when an order is updated. Configuration includes shipping status tags.

15-day free trial – unlimited tasks

Documentation

This task monitors order tags. For an order that has a single (non-cancelled) fulfillment, this task will update that fulfillment's shipment status according to the order's tags.

This task comes pre-configured with Shopify's shipment status identifiers on the right, and friendly order tags on the left. Feel free to update the tag names on the left-hand side. Do not modify the labels on the right! These must correspond directly to shipment statuses, as they're recorded by Shopify.

For a complete description of all Shopify shipment statuses, see Shopify's "shipment_status" documentation.

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 updated (shopify/orders/updated)
Options
shipping status tags (keyval, required)
Script
{% if event.preview %}
  {% action "shopify" %}
    [
      "post",
      "/admin/api/2019-04/orders/1234567890/fulfillments/1234567890/events.json",
      {
        "event": {
          "status": "confirmed"
        }
      }
    ]
  {% endaction %}
{% else %}
  {% assign open_fulfillments = array %}
  {% for fulfillment in order.fulfillments %}
    {% if fulfillment.status != "cancelled" %}
      {% assign index = open_fulfillments.size %}
      {% assign open_fulfillments[index] = fulfillment %}
    {% endif %}
  {% endfor %}

  {% if open_fulfillments.size == 0 %}
    {"log": "Order has no open fulfillments - this task can only manage orders that have 1."}
  {% elsif open_fulfillments.size > 1 %}
    {"log": "Order has {{ open_fulfillments.size }} open fulfillments - this task can only manage orders with 1."}
  {% else %}
    {% assign fulfillment = open_fulfillments.first %}
    {% assign order_tags = order.tags | split: ", " %}
    {% for order_tag in order_tags %}
      {% if options.shipping_status_tags__keyval_required[order_tag] == blank %}
        {% continue %}
      {% endif %}

      {% assign desired_status = options.shipping_status_tags__keyval_required[order_tag] %}
     
      {% if fulfillment.shipment_status == desired_status %}
        {"log": "Fulfillment status is already {{ desired_status }}; nothing to do"}
      {% else %}
        {% action "shopify" %}
          [
            "post",
            "/admin/api/2019-04/orders/{{ order.id }}/fulfillments/{{ fulfillment.id }}/events.json",
            {
              "event": {
                "status": {{ desired_status | json }}
              }
            }
          ]
        {% endaction %}
      {% endif %}

      {% break %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Shipping status tags
{"label_printed"=>"shipment_label_printed", "label_purchased"=>"shipment_label_purchased", "attempted_delivery"=>"shipment_attempted_delivery", "ready_for_pickup"=>"shipment_ready_for_pickup", "confirmed"=>"shipment_confirmed", "in_transit"=>"shipment_in_transit", "out_for_delivery"=>"shipment_out_for_delivery", "delivered"=>"shipment_delivered", "failure"=>"shipment_failure"}