importOrder
Write an order that already happened: the order, its items, its two addresses, its payment transactions and its activity log, in one database transaction. Use it to move the history of another system into EverShop.
A new order, written by createOrder, takes the quantity off the stock and writes the event order_created (which starts webhooks, e-mail and analytics). Both are wrong for an order that was placed a year ago. While importOrder runs, those two database triggers step aside; they work as usual for everything else.
Import
import { importOrder } from '@evershop/evershop/oms/services';
Syntax
importOrder(
data: HistoricalOrderInput,
options?: { connection?: PoolClient }
): Promise<ImportedOrder>
data
| Field | Type | Description |
|---|---|---|
currency | string | Required. For example USD. |
items | array | Required, at least one. See below. |
paymentStatus | string | Required. A key of oms.order.paymentStatus, for example paid. |
shipmentStatus | string | A shipment status of the store. Default: delivered when no item needs shipping, otherwise pending. |
createdAt | Date | string | When the order was placed. Default: now. |
orderNumber | string | Default: the next number, as a new order gets it. A number of only digits above 10000 is refused: the numbers 10001 and up are the ones that new orders will get, and a collision would make checkout fail. Keep an old number with a prefix, for example IMP-10050. |
customer | object | { id?, email?, fullName? }. The customer is not looked up; id is stored as given. |
coupon | string | The coupon code. Its use is counted (used_time goes up by one). |
shippingAddress, billingAddress | object | The columns of order_address (recipient, address_line_1, locality, administrative_area, postal_code, country, …). Stored as given: only country must be a country EverShop knows. |
shipping | object | { methodName?, methodData?, feeExclTax?, feeInclTax?, taxAmount?, note? }. A fee given with tax only has the tax taken out of it (taxAmount, else none); a fee given without tax has taxAmount added. |
paymentMethod, paymentMethodName | string | For example stripe and Credit Card. |
transactions | array | Payment transactions: { transactionId?, type, amount, paymentAction, parentTransactionId?, additionalInformation?, createdAt? }. A transaction without transactionId is stored without one (several are allowed). |
activity | array | Extra lines for the order's history: { comment, customerNotified?, createdAt? }. |
metaData | object | Stored in the order's meta_data. |
Each entry of items:
| Field | Type | Description |
|---|---|---|
sku | string | Required. The product is found by its sku and must exist in the store. Use productId to name the product by id instead. |
name | string | Required. The name on the order (it need not match the product today). |
qty | integer | Required, above 0. |
price | number | Required. The unit price without tax. |
priceInclTax, taxPercent | number | The unit price with tax and the tax rate. Give either: the other is worked out. With neither, the item has no tax. If both are given they must agree to one cent. |
discountAmount | number | The discount of the whole line, without tax. Default 0, at most the line. It comes off before the tax is worked out. |
weight, thumbnail, variantOptions | Stored on the order item. | |
noShippingRequired | boolean | For a download or a service. |
options.connection
By default importOrder opens its own transaction. To make it part of yours, pass a connection that is already in a transaction (started with startTransaction of the query builder); the function refuses any other. You commit or roll back, also when a hook throws: the order is then still in your transaction until you roll it back. The "do not take stock" flag lasts until your transaction ends, so do not write a new order on the same transaction after the call.
Return value
{
orderId: number;
uuid: string;
orderNumber: string;
status: string; // resolved from the payment and shipment statuses
paymentStatus: string;
shipmentStatus: string;
}
What it does, and what it does not
- The order status is resolved from the payment and shipment statuses with the store's mapping (see Order Status Management); it is never given by hand. A status the store does not have is an error.
- The amounts are worked out from the items and the shipping you give, not from today's prices, taxes or shipping rules: the order is a record of what was charged. The order totals are the sum of the items, so they agree with them.
- The stock is not changed and the event
order_createdis not written. No e-mail is sent. placed_atis set tocreatedAt, unless the payment status ispending,canceledor a failed status, no transaction is given and the payment method is notcod: such an order was never placed. A Cash on Delivery order is placed when it is created.order_placedis not written (see markOrderPlaced).- Nothing is written when the data is not an order (no item, a sku the store does not have, a country EverShop does not know, an unknown status, an amount that is not a number, a discount above its line), or, in its own transaction, when a hook throws.
- A closed placeholder cart is created, because an order must belong to a cart.
Example
import { importOrder } from '@evershop/evershop/oms/services';
const order = await importOrder({
currency: 'USD',
createdAt: '2023-03-04T05:06:07Z',
customer: { email: 'jane@example.com', fullName: 'Jane Doe' },
items: [
{ sku: 'MUG-BLUE', name: 'Blue mug', qty: 2, price: 10, taxPercent: 8 }
],
shippingAddress: {
recipient: 'Jane Doe',
address_line_1: '1 Main Street',
locality: 'Springfield',
administrative_area: 'US-IL',
postal_code: '62701',
country: 'US'
},
shipping: { methodName: 'Ground', feeExclTax: 5, feeInclTax: 5 },
paymentMethod: 'stripe',
paymentStatus: 'paid',
shipmentStatus: 'delivered',
transactions: [
{ transactionId: 'pi_123', type: 'online', amount: 26.6, paymentAction: 'capture' }
]
});
// order.status === 'completed' with the default order status mapping
Hooks
importOrder is hookable. Register from bootstrap.ts, with a function, not an arrow function.
import {
hookBeforeImportOrder,
hookAfterImportOrder
} from '@evershop/evershop/oms/services';
// Before the writes: (data, connection)
hookBeforeImportOrder(async function (data, connection) {
// …
});
// After the writes, in the same transaction: (result, data, connection).
// The result comes first. Throwing here rolls the whole order back (when
// importOrder opened the transaction; with your connection, you roll back).
hookAfterImportOrder(async function (result, data, connection) {
// …
});
See Also
- createOrder — Creates an order from a cart
- addOrderActivityLog — Adds a line to an order's history
- addPaymentTransaction — Records a payment transaction
- Order Status Management — How the order status is resolved