Auto-fulfill items that don't require shipping, with Mechanic.

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

Auto-fulfill items that don't require shipping

Useful for digital products, memberships, or anything else that needs to be fulfilled instantly. This task watches for paid orders, and auto-fulfills all line items that don't require shipping.

Runs Occurs whenever an order is paid and Occurs when a user sends an order to Mechanic. Configuration includes include products with any of these tags, exclude products with any of these tags, and wait until any other shippable items are fulfilled.

15-day free trial – unlimited tasks

Documentation

Useful for digital products, memberships, or anything else that needs to be fulfilled instantly. This task watches for paid orders, and auto-fulfills all line items that don't require shipping.

Optionally, choose to include or exclude products by tag, and wait until other shippable items are fulfilled, if any.

Note: exclusion tags on a product will take priority over inclusion tags.

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/paid
{% if options.wait_until_any_other_shippable_items_are_fulfilled__boolean %}
  shopify/orders/partially_fulfilled
  shopify/orders/fulfilled
{% endif %}
mechanic/user/order
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
include products with any of these tags (array), exclude products with any of these tags (array), wait until any other shippable items are fulfilled (boolean)
Code
{% assign inclusion_tags = options.include_products_with_any_of_these_tags__array %}
{% assign exclusion_tags = options.exclude_products_with_any_of_these_tags__array %}
{% assign wait_until_any_other_shippable_items_are_fulfilled = options.wait_until_any_other_shippable_items_are_fulfilled__boolean %}

{% comment %}
  -- get all open or in progress fulfillment orders
{% endcomment %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      fulfillmentOrders(
        first: 10
        query: "status:open OR status:in_progress"
      ) {
        nodes {
          id
          assignedLocation {
            location {
              id
            }
          }
          lineItems(first: 30) {
            nodes {
              id
              remainingQuantity
              requiresShipping
              variant {
                product {
                  tags
                }
              }
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "fulfillmentOrders": {
            "nodes": [
              {
                "id": "gid://shopify/FulfillmentOrder/1234567890",
                "assignedLocation": {
                  "location": {
                    "id": "gid://shopify/Location/1234567890"
                  }
                },
                "lineItems": {
                  "nodes": [
                    {
                      "id": "gid://shopify/FulfillmentOrderLineItem/1234567890",
                      "remainingQuantity": 1,
                      "requiresShipping": false,
                      "variant": {
                        "product": {
                          "tags": {{ inclusion_tags.first | json }}
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign fulfillment_orders = result.data.order.fulfillmentOrders.nodes %}

{% if fulfillment_orders == blank %}
  {% log "There are no open fulfillment orders to fulfill on this order." %}
  {% break %}
{% endif %}

{% comment %}
  NOTE: fulfillments can only be created for one location at a time, so need to group fulfillment orders by location
{% endcomment %}

{% assign fulfillment_orders_by_location = hash %}
{% assign has_unfulfilled_shippable_items = nil %}

{% for fulfillment_order in fulfillment_orders %}
  {% assign fulfillment_order_data = hash %}
  {% assign fulfillment_order_data["fulfillment_order_id"] = fulfillment_order.id %}

  {% for fulfillment_order_line_item in fulfillment_order.lineItems.nodes %}
    {% comment %}
      -- skip items that do not require shipping, but set flag if any are unfulfilled
    {% endcomment %}

    {% if fulfillment_order_line_item.requiresShipping %}
      {% if fulfillment_order_line_item.remainingQuantity > 0 %}
        {% assign has_unfulfilled_shippable_items = true %}
      {% endif %}

      {% continue %}
    {% endif %}

    {% if inclusion_tags != blank or exclusion_tags != blank %}
      {% assign has_exclusion_tag = nil %}
      {% assign has_inclusion_tag = nil %}

      {% assign product_tags = fulfillment_order_line_item.variant.product.tags %}

      {% for exclusion_tag in exclusion_tags %}
        {% if product_tags contains exclusion_tag %}
          {% assign has_exclusion_tag = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% for inclusion_tag in inclusion_tags %}
        {% if product_tags contains inclusion_tag %}
          {% assign has_inclusion_tag = true %}
          {% break %}
        {% endif %}          
      {% endfor %}

      {% comment %}
        -- exclusion tags have priority over inclusion tags
      {% endcomment %}

      {% if has_exclusion_tag %}
        {% log
          message: "This line item's product has at least one of the configured exclusion tags; skipping",
          fulfillment_order_line_item: fulfillment_order_line_item,
          product_tags: product_tags,
          exclusion_tags: exclusion_tags
        %}
        {% continue %}

      {% elsif inclusion_tags != blank %}
        {% unless has_inclusion_tag %}
          {% log
            message: "This line item's product does not contain any of the configured inclusion tags; skipping",
            fulfillment_order_line_item: fulfillment_order_line_item,
            product_tags: product_tags,
            inclusion_tags: inclusion_tags
          %}
          {% continue %}
        {% endunless %}
      {% endif %}
    {% endif %}

    {% comment %}
      -- save unfulfilled line items that do not require shipping
    {% endcomment %}

    {% if fulfillment_order_line_item.remainingQuantity > 0 %}
      {% assign fulfillment_order_data["unfulfilled_line_items"]
        = fulfillment_order_data["unfulfilled_line_items"]
        | default: array
        | push: fulfillment_order_line_item
      %}
    {% endif %}
  {% endfor %}

  {% comment %}
    -- group unfulfilled line items by location for fulfillment
  {% endcomment %}

  {% if fulfillment_order_data.unfulfilled_line_items != blank %}
    {% assign fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]
      = fulfillment_orders_by_location[fulfillment_order.assignedLocation.location.id]
      | default: array
      | push: fulfillment_order_data
    %}
  {% endif %}
{% endfor %}

{% if wait_until_any_other_shippable_items_are_fulfilled and has_unfulfilled_shippable_items %}
  {% log "Unfulfilled shippable items exist on this order and the 'Wait until any other shippable items are fulfilled' option is checked; no auto fulfillments will be made in this task run." %}
  {% break %}
{% endif %}

{% for keyval in fulfillment_orders_by_location %}
  {% action "shopify" %}
    mutation {
      fulfillmentCreate(
        fulfillment: {
          lineItemsByFulfillmentOrder: [
            {% for fulfillment_order_data in keyval[1] %}
              {
                fulfillmentOrderId: {{ fulfillment_order_data.fulfillment_order_id | json }}
                fulfillmentOrderLineItems: [
                  {% for unfulfilled_line_item in fulfillment_order_data.unfulfilled_line_items %}
                    {
                      id: {{ unfulfilled_line_item.id | json }}
                      quantity: {{ unfulfilled_line_item.remainingQuantity }}
                    }
                  {% endfor %}
                ]
              }
            {% endfor %}
          ]
          notifyCustomer: false
        }
      ) {
        fulfillment {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more