> ## Documentation Index
> Fetch the complete documentation index at: https://www.twicecommerce.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Auto-Fulfillment

> How TWICE Commerce automatically assigns stock items to orders based on service location fulfillment rules.

<Frame caption="Workflows">
  <img src="https://mintcdn.com/twicecommerce/lZhc_tO8u1u_bL0Q/images/workflows.webp?fit=max&auto=format&n=lZhc_tO8u1u_bL0Q&q=85&s=1799912ecc09f71e36a801fcc5e1f46d" alt="Workflows view used for auto-fulfillment" width="1920" height="1080" data-path="images/workflows.webp" />
</Frame>

## Definition

Auto-fulfillment automatically assigns an available [Stock Item](/docs/concepts/inventory/stock-item-state) to each incoming order line at the moment the order is created. When enabled on a Service Location, the system picks a Stock Item from that location's inventory without requiring a staff member to choose one manually. Auto-fulfillment is what makes high-volume online channels viable — every booking that comes in already has stock attached and is ready to be prepared.

## Where do I use it?

* High-volume online stores where manual stock selection is impractical
* Self-service and unstaffed pickup points
* Multi-location operations where a default location should claim orders first
* Workflows that depend on Stock Item assignment existing at order creation
* Reducing the time between order creation and a fulfillable order in the queue

## Key Properties

Auto-fulfillment is configured per [Service Location](/docs/concepts/admin/locations) under **Settings → Service locations → \[Location] → Fulfillment**. The configuration has three layers:

| Setting                                                                                     | What it does                                                                                       |
| :------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------- |
| **Order fulfillment** (`orderFulfillmentEnabled`)                                           | Master switch — does this location participate in outgoing fulfillment?                            |
| **Order returns** (`orderReturnsEnabled`)                                                   | Master switch — does this location accept returns?                                                 |
| **Use specific rules** (`orderFulfillmentUseSpecificRules`, `orderReturnsUseSpecificRules`) | When on, rules differ per purchase type. When off, one rule applies to everything.                 |
| **Auto-assign** (`useAutoAssign`)                                                           | When on, Stock Items are picked automatically at order creation. When off, staff must assign them. |

### Delivery methods per rule

Each fulfillment or return rule can enable one or more delivery methods:

| Method              | Field                  | Description                                     |
| :------------------ | :--------------------- | :---------------------------------------------- |
| **In-store pickup** | `inStoreEnabled`       | Customer collects from the location.            |
| **Shipping**        | `shippingEnabled`      | Carrier dispatch from / return to the location. |
| **Local delivery**  | `localDeliveryEnabled` | The merchant delivers / picks up directly.      |

### Per-purchase-type rules

When **Use specific rules** is enabled, separate rule sets exist for each [purchase type](/docs/concepts/orders/order-types):

* **Booking** (`booking`) — for rental orders
* **Purchase** (`sale`) — for sale orders
* **Subscription** — for recurring orders
* **Buyback** — for buyback orders

A warehouse might accept all four; a downtown showroom might only accept in-store pickup for Booking and Purchase.

## Relationships

* **Service Location** → owns the fulfillment and return configuration.
* **Stock Items** → the inventory pool that auto-assign draws from. Only items at the matching location and matching the Listing's variant are eligible.
* **Order Items** → on creation, each item is paired with one or more Stock Items if auto-assign is active.
* **Workflows** → can react to fulfillment events (assignment, pickup, return) — see [Workflows](/docs/workflows).
* **Channels** → a Service Location can be restricted to certain sales channels via `salesChannelAdmin` and `salesChannelOnline`.

## Lifecycle

### What triggers auto-fulfillment

Auto-fulfillment runs when an Order is created and at least one Service Location has:

1. `orderFulfillmentEnabled = true`
2. `useAutoAssign = true`
3. A rule for the line item's purchase type with at least one delivery method enabled
4. Available Stock Items matching the line item's product and variant

If multiple locations qualify, the system picks the first match based on the location ordering. If no location qualifies, the line item is created with no Stock Item assigned and shows as **Unfulfilled** until a staff member assigns one.

### Step-by-step

<Steps>
  <Step title="Order arrives">
    A customer checks out online, or an Order is created via the API or admin.
  </Step>

  <Step title="Eligible locations enumerated">
    TWICE finds Service Locations with `orderFulfillmentEnabled` and the relevant channel flag (`salesChannelOnline`, etc.) set.
  </Step>

  <Step title="Rules matched">
    For each line item, the system reads the rule matching the line's `purchaseType`. The line proceeds only if at least one delivery method is enabled on that rule.
  </Step>

  <Step title="Auto-assign picks a Stock Item">
    If `useAutoAssign` is on, TWICE picks an available Stock Item from the location's inventory that matches the line item's product and variant for the requested rental period.
  </Step>

  <Step title="Order saved">
    The Stock Item is attached to the line item in fulfillment state `pending` — assigned, but still replaceable by the switching logic. The order-level rollup stays **Unfulfilled** until staff prepare or hand over the items.
  </Step>
</Steps>

### Key behaviours

* **Auto-assign is per location.** A warehouse can auto-assign while a showroom stays manual.
* **Rules are per purchase type.** A location can accept in-store pickup for Bookings but require shipping for Purchases.
* **Fulfillment and returns are configured separately.** Enabling order fulfillment does not automatically enable returns. A location can ship orders out without accepting in-bound returns at the same time.
* **No delivery method = no auto-assign.** The system requires at least one of `inStoreEnabled`, `shippingEnabled`, or `localDeliveryEnabled` on the matching rule. Enabling auto-assign with no rules configured leaves orders without a delivery method.
* **No available Stock Item = unfulfilled.** Auto-assign never overbooks. If the pool is empty for the requested period, the line stays unfulfilled and a staff member must intervene.

### Channel-specific behaviour

Service Locations carry per-channel flags. Auto-fulfillment respects these:

| Flag                 | When checked                 |
| :------------------- | :--------------------------- |
| `salesChannelAdmin`  | Orders created in admin      |
| `salesChannelOnline` | Orders from the online store |

A location that only has `salesChannelOnline = true` never auto-fulfills admin-created orders.

## FAQs

<AccordionGroup>
  <Accordion title="What happens when I enable auto-assign with no fulfillment rules?">
    Orders are created without a delivery method and remain **Unfulfilled**. Always configure at least one delivery method (in-store, shipping, or local delivery) on the rule for each purchase type you support at that location.
  </Accordion>

  <Accordion title="Can different locations auto-fulfill the same order?">
    A single Order Item is assigned to one Stock Item at one location. Different line items in the same Order can be fulfilled from different locations. The selection is per-line, not per-order.
  </Accordion>

  <Accordion title="Why didn't my order get an auto-assignment?">
    Common causes: the matching purchase type has no rule, all delivery methods on the rule are disabled, the location's channel flag does not match the order's channel, no Stock Items are available for the requested period, or `useAutoAssign` is off on the location.
  </Accordion>

  <Accordion title="Can I auto-fulfill returns the same way?">
    Returns use the **Order returns** section with the same structure (per-purchase-type rules and per-method switches). There is no separate auto-assign for returns — when an item comes back, it's matched to the existing Order Item rather than picked from inventory.
  </Accordion>

  <Accordion title="Does auto-fulfillment respect the cancellation policy?">
    Auto-fulfillment runs before cancellation policies become relevant. If an order is cancelled later, the assigned Stock Item is released back to inventory regardless of how it was originally assigned.
  </Accordion>
</AccordionGroup>

## Developer Reference

Auto-fulfillment rules live on `service-locations` in the API.

<Card title="API: Service Locations" icon="code" href="https://server.twicecommerce.com/api/internal">
  Open the endpoint in the API reference.
</Card>

## Related

<CardGroup cols={2}>
  <Card title="Order Lifecycle" icon="rotate" href="/docs/concepts/orders/order-lifecycle">
    How orders move through statuses from creation to completion.
  </Card>

  <Card title="Order Types" icon="tags" href="/docs/concepts/orders/order-types">
    Rental, sale, subscription, and buyback orders.
  </Card>

  <Card title="Stock Item State" icon="box" href="/docs/concepts/inventory/stock-item-state">
    How Stock Items move when assigned and released.
  </Card>

  <Card title="Service Locations" icon="building" href="/docs/concepts/admin/locations">
    Where fulfillment rules live.
  </Card>
</CardGroup>
