Manage fulfillment shipment status using order tags, with Mechanic.

Mechanic is a development platform for Shopify. :)

Manage fulfillment shipment status using order tags

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. Optionally, you may have the task attempt to fulfill the order first and may choose whether to send shipment status notifications to the customer.

Runs when an order is updated. Configuration includes shipment tags and status, attempt to fulfill the order before tagging, and send shipment notifications to customer.

15-day free trial – unlimited tasks

Documentation

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. Optionally, you may have the task attempt to fulfill the order first and may choose whether to send shipment status notifications to the customer.

This task monitors order tags. For an order that has a single fulfillment, this task will update that fulfillment's shipment status according to the order's tags. If the fulfill option is chosen, this task will only attempt to fulfill orders with no prior fulfillments (e.g., it will ignore orders that have been fulfilled, then cancelled, and then tagged).

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.

Open source
View on GitHub to contribute to this task
Events
when an order is updated (shopify/orders/updated)
Options
shipment tags and status (keyval, required), attempt to fulfill the order before tagging (boolean), send shipment notifications to customer (boolean)
Script
{% if event.preview %}
  {% capture order_json %}
    {
      "id": 1234567890,
      "fulfillments": [
        {
          "id": 23456778901,
          "status": "open"
        }
      ],
      "tags": {{ options.shipment_tags_and_status__keyval_required | keys | first | json }}
    }
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% endif %}

{% assign order_tags = order.tags | split: ", " %}

{% if order.fulfillments == blank and options.attempt_to_fulfill_the_order_before_tagging__boolean %}
  {% for order_tag in order_tags %}
    {% if options.shipment_tags_and_status__keyval_required[order_tag] != blank %}
      {% action "shopify" %}
        [
          "post",
          "/admin/api/{{ task.shopify_api_version }}/orders/{{ order.id }}/fulfillments.json",
          {
            "fulfillment": {
              "location_id": {{ shop.primary_location_id | json }},
              "tracking_number": null,
              "notify_customer": {{ options.send_shipment_notifications_to_customer__boolean }}
            }
          }
        ]
      {% endaction %}

      {% break %}
    {% endif %}
  {% endfor %}

  {% log "No shipment status tags found on this order; skipping" %}
{% else %}
  {% assign open_fulfillments = array %}

  {% for fulfillment in order.fulfillments %}
    {% if fulfillment.status != "cancelled" %}
      {% assign open_fulfillments[open_fulfillments.size] = 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."
    {% endlog %}
  {% else %}
    {% assign fulfillment = open_fulfillments.first %}

    {% for order_tag in order_tags %}
      {% if options.shipment_tags_and_status__keyval_required[order_tag] == blank %}
        {% continue %}
      {% endif %}

      {% assign desired_status = options.shipment_tags_and_status__keyval_required[order_tag] %}

      {% if fulfillment.shipment_status == desired_status %}
        {% log %}
          "Fulfillment status is already {{ desired_status }}; nothing to do"
        {% endlog %}
      {% else %}
        {% action "shopify" %}
          [
            "post",
            "/admin/api/{{ task.shopify_api_version }}/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
Shipment tags and status
{"shipment_label_printed"=>"label_printed", "shipment_label_purchased"=>"label_purchased", "shipment_attempted_delivery"=>"attempted_delivery", "shipment_ready_for_pickup"=>"ready_for_pickup", "shipment_confirmed"=>"confirmed", "shipment_in_transit"=>"in_transit", "shipment_out_for_delivery"=>"out_for_delivery", "shipment_delivered"=>"delivered", "shipment_failure"=>"failure"}