A Core dna store is a set of records and an API that moves a purchase from cart to order: products described in every language and priced in every currency the store sells in, a cart, a checkout that becomes an order, invoices, payments, tax and shipping. The site builds the storefront on top: the cart and checkout pages, their scripts, and the customer emails.
L2–L3
Unit 05 · From payment to fulfilment
Stock, fulfilment and the member's order screens
From payment to fulfilmentPage 21 of 31Updated
How stock is recorded per warehouse and kept in step, how the platform chooses a warehouse and holds stock while a shopper pays, how fulfilments and shipments follow an order out of the door, which status a customer sees, and how to build the member's order screens.
Who this is for. Developers connecting a store to its stock and dispatch systems, and building the order pages of the member area. After this page you can keep stock figures right, show availability, track an order's journey, and override the two order screens.
Stock is a ledger per warehouse
Stock is recorded in four records:
Record
What it holds
Warehouse
A place stock is kept, with a unique code and an address.
Inventory transaction
One movement of stock: the SKU, the warehouse, the quantities moved and the reason. It can belong to a batch, and can carry an expiry, for a reservation held before payment. A transaction is final once written.
Stock
One product's or variant's quantities in one warehouse: available, reserved, in transit and on hand. It is the running total of that product's transactions in that warehouse, kept up to date by the platform as transactions are written.
Batch
A lot of goods, with a name, an expiry date and a received date, for lot and expiry tracking.
The four quantities let the store say where every unit is: available to sell, reserved for an order that has not left yet or a checkout being paid for right now, in transit to a warehouse or a customer, and on hand on the shelf. A product and each variant also carry their available quantity summed across the account's warehouses, which templates read with getAvailableQuantity().
Keeping stock in step
Stock changes when a transaction is written. Staff and other systems write one in two ways:
The product editor. Staff enter the four quantities per warehouse as they are now; on save, the platform writes one transaction per warehouse holding the difference. A form opened before someone else changed the stock is refused, so two people never overwrite each other's counts.
The Inventory API.POST /cdna-api/inventory/transaction records one movement for a SKU in a warehouse: the quantity moved in each bucket and a reason (Record a transaction).
With Enable inventory tracking on, the platform also writes transactions itself: it holds the stock of each order at purchase and moves it on at each step of the order's fulfilment (Warehouse allocation and stock holds). Choose the set-up by who decides when stock leaves:
Tracking on: the platform owns the orders. It checks stock in the cart and at checkout, holds it for each paid order, and draws it down as the fulfilment moves on. The system that ships the goods, such as a warehouse or ERP system, writes through the API only the movements the platform does not know about: goods received add to on hand and available, and a return put back on the shelf adds them again. It reports each order's progress by moving the fulfilment through its lifecycle (Fulfilments and shipments), not by writing the order's movements as transactions, which the platform has already written.
Tracking off, the default: the system that ships the goods owns the numbers. It writes a transaction through the API for each movement it makes. Goods received add to on hand and available; an order picked moves units from available to reserved; a parcel handed to the courier moves them out; a return put back on the shelf adds them again. When to reserve and when to take units out of stock is that system's rule; tell it about each paid order with a job on the order (Entity events).
Either way, the store reads stock and never has to calculate it.
A transaction that makes a product available again fires the back-in-stock event, which a job turns into emails to the people who asked (Back-in-stock requests).
Selling limited stock
A store that sells limited stock shows availability on the product page and offers the add button only while something is available:
{% set product = productDescription.getProduct() %}
{% if product.getAvailableQuantity() > 0 %}
<p class="stock">In stock</p>
<button type="submit">Add to cart</button>
{% else %}
<p class="stock">Out of stock</p>
{% include 'products/includes/back-in-stock-form.html' %}
{% endif %}
The include is the store's own back-in-stock form. For a product with variants, read each variant's own getAvailableQuantity() and offer only the options that have stock (The product page and its variants). Where the last units of something can sell twice at the same moment, as in a flash sale, inventory tracking holds each unit for one checkout before the payment is taken, so two shoppers paying at once cannot both hold the last one (When stock is held). With tracking off, the system that ships the goods has the final word: it confirms each paid order against the shelf, and an item it cannot supply is refunded and the customer told (Refunds).
Warehouse allocation and stock holds
With Enable inventory tracking on (Transactions › Configuration › Inventory), the platform does three things for every order: it chooses the warehouse each line leaves from, holds the stock while the shopper pays, and moves the stock on at each step of the fulfilment. The setting is off by default. With it off, the platform checks and holds no stock, and each checkout gets one fulfilment for all its items: at the chosen warehouse for a pickup, and without a warehouse for a delivery.
Choosing the warehouse
The warehouses are chosen when shipping is created at checkout, from the account's active warehouses that serve the site, and only where an active shipping method can send each product:
Delivery follows Delivery allocation strategy. The default, "Closest warehouse that covers the whole order", ranks the warehouses by distance to the delivery address and takes the nearest one that holds every item. "Closest warehouse in the customer's home region, then anywhere" tries the warehouses in the customer's own region first. Either way a delivery is not split, with one exception: an item whose shipping methods only ship from certain warehouses leaves from one of those, and the order is split only when none of them can send the rest as well.
Pickup follows Click and collect allocation strategy, "Customer's chosen warehouse": the warehouse the shopper chose takes every item it holds. When a pickup shipping group has Same warehouse switched off, a shopper whose warehouse is short of an item can collect the rest from one other warehouse.
Products whose shipping category has Separate fulfillment on get a fulfilment of their own, with its own shipping, even when they leave from the same warehouse as the rest.
Products whose shipping methods all have Nothing to ship on, such as gift cards and courses, hold no stock: they get a fulfilment of their own without a warehouse.
Distance needs coordinates. With a Google Geocoding API key (Content › Common › Configuration), warehouse addresses are geocoded when they are saved, and a delivery address when shipping is created. Without one, the warehouses are ranked by matching postcode, then state, then country.
A checkout can so carry several fulfilments, each with its own shipping rates, and the shopper chooses a rate for each (Creating shipping and showing the rates).
When stock is held
Stock is checked at each step of the checkout, and held once, at purchase:
Step
What happens
The cart
A line that asks for more than any one warehouse holds, or a product whose lines together ask for more than all the warehouses hold, gets a warning message. The line stays in the cart.
Creating the checkout
Refused for the same reason.
Creating shipping
The warehouses are chosen; refused when the lines cannot be placed for this address or collection warehouse. Nothing is held yet, so a shopper can switch between delivery and pickup, or go back to shopping, without tying stock up.
Purchase
Before the payment is taken, each line's stock is held at its warehouse: it moves from available to reserved. The hold lasts the checkout lifetime (Checkout Lifetime, Checkouts.lifetime, 1,800 seconds) from the purchase request, and the next attempt after a declined payment renews it. If the stock went to another order since shipping was created, purchase is refused and the shopper chooses shipping again.
The order
The hold becomes permanent: the units stay reserved until the fulfilment moves on.
A hold is released when the payment does not finish within the checkout lifetime. A payment that completes after its hold has run out still becomes an order and takes the stock then; when another order took those units in the meantime, the movement is recorded with the reason checkout_reservation_oversold, so staff can see it. What each refusal looks like to the checkout page is on When stock is short at checkout.
How the stock moves
Each step writes one inventory transaction per line at the line's warehouse, with a reason; q is the line's quantity:
Step
Available
Reserved
In transit
On hand
Reason
Purchase
−q
+q
checkout_reservation
A fulfilment added by staff to a placed order
−q
+q
fulfillment_reservation
Ready, or ready for collection
No movement: the units stay reserved on the shelf
In transit (delivery)
−q
+q
−q
fulfillment_shipped
Delivered (delivery)
−q
fulfillment_delivered
Collected (pickup)
−q
−q
fulfillment_collected
Cancelled
+q
−q
fulfillment_cancelled
Delivered digitally, for a line that held stock
+q
−q
fulfillment_nothing_to_ship
So on hand stays available plus reserved for every unit still in the building. A fulfilment made while tracking was off holds no stock and moves none, whatever the setting says later.
Fulfilments and shipments
A fulfilment is a set of an order's lines that leave together. It is created at checkout when shipping is created (Creating shipping and showing the rates): with inventory tracking off, one for all available items; with it on, one for each warehouse the allocation chose, for each Separate fulfillment shipping category and for the lines with nothing to ship (Warehouse allocation and stock holds). The fulfilments move to the order at the conversion, and staff can add one to a placed order for items no fulfilment covers. A fulfilment holds:
its items: each order line it covers, with a quantity (getFulfillmentItems());
its type, delivery or pickup (getType()), and a label for it, "Delivery", "Pickup", or "Digital" when there is nothing to ship (getTypeDisplay());
a delivery address for a delivery, and a warehouse: the one a pickup is collected at, and with inventory tracking on the one a delivery leaves from (getDeliveryAddress(), getWarehouse());
the shipping rate the shopper chose (getSelectedShippingRate());
its status, and its shipments.
A fulfilment moves through its statuses one step at a time. Staff use the buttons on its card on the order page in the admin, and other systems use the fulfilment routes of the Inventory API; a status is never written directly. Each step is logged with who made it, and with inventory tracking on it moves the stock (How the stock moves). No step emails the customer: a job on the fulfilment does that (Customer emails).
Value
Label
Means
pending
Pending
Created; nothing picked yet
ready
Ready; "Ready for collection" for a pickup
Picked and packed, waiting for the carrier or for the customer
in_transit
In Transit
With the carrier; deliveries only
delivered
Delivered; "Collected" for a pickup, "Delivered digitally" when there is nothing to ship
The customer has the goods
cancelled
Cancelled
Stopped before it left the warehouse
A delivery goes pending → ready → in_transit → delivered. A pickup goes pending → ready → delivered, and is never in transit. Either can be cancelled while it is pending or ready; with inventory tracking on its stock goes back to available. Cancelling does not refund the customer, which is what refunding the order is for. A fulfilment whose shipping method has Nothing to ship is marked delivered as soon as its order is paid. getStatusDisplay() gives the label in the fulfilment's own wording, and isPending(), isReady(), isInTransit(), isDelivered() and isCancelled() test the status.
A shipment is one parcel on its way. It belongs to a delivery fulfilment and carries the courier's trackingCode and trackingUrl, an externalId and what the parcel cost to send. The first shipment added to a ready fulfilment moves it to in transit, and a fulfilment whose items leave in more than one parcel has a shipment for each. A pickup has no shipments. getSafeTrackingUrl() returns the tracking URL only when it is an http or https address, so it is the one to put in a link. An order can also have more than one fulfilment, each with its own status: that is a partial fulfilment, where some lines ship before others.
The status a customer sees
The order's fulfilment status sums up its fulfilments, and is worked out again every time the order is saved. A cancelled fulfilment is left out of the count. Items that no active fulfilment covers, such as the lines of a cancelled fulfilment until staff add them to a new one, count as one more pending fulfilment. getFulfillmentStatus() returns the value and getFulfillmentStatusDisplay() the label:
Value
Label
When
none
Not Fulfilled
No fulfilments, or all of them cancelled
pending
Pending Fulfillment
Every fulfilment is pending
partial
Partially Fulfilled
Some fulfilments are pending and others have moved on
fulfilled
Fulfilled
None is pending: each is ready, in transit or delivered, and not all are delivered
completed
Fulfillment Complete
Every fulfilment is delivered
An order of nothing but goods with nothing to ship is complete as soon as it is paid. Show the customer the order's fulfilment status as the summary, and beside the items each fulfilment covers, its own status and tracking links.
The member's order screens
The member area has two order screens, and each shows only the signed-in member's own orders:
Screen
Template
Receives
The member's orders, a page at a time
orders/orders/index.html
orders
One order
orders/orders/show.html
order
The framework ships starter versions of both, which extend users/frontend/includes/account.html. Override them by creating the same paths in the site's templates/ folder, and print every amount with the money getters and format(), which format in the order's own currency. Link a row to its order with $_url->getPath('MyAccount', 'orderDetails', ['id' => $order->getId()]), which builds the member area's path for that order.
Template codetemplates/orders/orders/index.html
<table>
{foreach $orders as $order}
{$ordered = $order->getOrdered()}
<tr>
<td><a href="{$_url->getPath('MyAccount', 'orderDetails', ['id' => $order->getId()])}">{$order->getNumber()|escape}</a></td>
<td>{if $ordered}{$ordered->format('j M Y')}{/if}</td>
<td>{$order->getFinancialStatusDisplay()}</td>
<td>{$order->getFulfillmentStatusDisplay()}</td>
<td>{$order->getTotalAsMoney()->format()}</td>
</tr>
{foreachelse}
<tr><td colspan="5">You have no orders yet.</td></tr>
{/foreach}
</table>
<table>
{% for order in orders %}
<tr>
<td><a href="{{ _url.getPath('MyAccount', 'orderDetails', {'id': order.getId()}) }}">{{ order.getNumber()|e }}</a></td>
<td>{{ order.getOrdered() ? order.getOrdered()|date('j M Y') }}</td>
<td>{{ order.getFinancialStatusDisplay() }}</td>
<td>{{ order.getFulfillmentStatusDisplay() }}</td>
<td>{{ order.getTotalAsMoney().format() }}</td>
</tr>
{% else %}
<tr><td colspan="5">You have no orders yet.</td></tr>
{% endfor %}
</table>
On the order screen, each line's stored name is in the client's default language. On a multilingual site, take the name from the line's product description in the visitor's language, keeping the stored name as the fallback, as Reading the right description does for product cards. Fulfilments follow the lines, each with its type and status in its own wording, and a tracking link for each shipment that has a web address:
{% set language = _query.findOneBy('Common\\Entities\\Language', {'code': _request.getLanguage()}) %}
<h1>Order {{ order.getNumber()|e }}</h1>
<p>{{ order.getFinancialStatusDisplay() }} · {{ order.getFulfillmentStatusDisplay() }}</p>
<table>
{% for item in order.getItems() %}
{% set name = item.getName() %}
{% set product = item.getProduct() %}
{% set description = language and product ? product.getDescriptionForLanguage(language) : null %}
{% if description %}{% set name = description.getName() %}{% endif %}
<tr><td>{{ name|e }} × {{ item.getQuantity() }}</td><td>{{ item.getTotalAsMoney().format() }}</td></tr>
{% endfor %}
<tr><td>Shipping</td><td>{{ order.getShippingTotalAsMoney().format() }}</td></tr>
<tr><td>Tax</td><td>{{ order.getTaxTotalAsMoney().format() }}</td></tr>
<tr><td>Total</td><td>{{ order.getTotalAsMoney().format() }}</td></tr>
</table>
{% for fulfillment in order.getFulfillments() %}
<h2>{{ fulfillment.getTypeDisplay() }}: {{ fulfillment.getStatusDisplay() }}</h2>
{% for shipment in fulfillment.getShipments() %}
{% if shipment.getSafeTrackingUrl() %}
<p><a href="{{ shipment.getSafeTrackingUrl()|e }}">Track parcel {{ shipment.getTrackingCode()|e }}</a></p>
{% endif %}
{% endfor %}
{% endfor %}
The order's refunds and the total refunded so far read the same way (The adjustment is meant to go negative). How the member area routes these screens and checks the signed-in member is Guide 7's subject.