Skip to main content

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​

FieldTypeDescription
currencystringRequired. For example USD.
itemsarrayRequired, at least one. See below.
paymentStatusstringRequired. A key of oms.order.paymentStatus, for example paid.
shipmentStatusstringA shipment status of the store. Default: delivered when no item needs shipping, otherwise pending.
createdAtDate | stringWhen the order was placed. Default: now.
orderNumberstringDefault: 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.
customerobject{ id?, email?, fullName? }. The customer is not looked up; id is stored as given.
couponstringThe coupon code. Its use is counted (used_time goes up by one).
shippingAddress, billingAddressobjectThe 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.
shippingobject{ 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, paymentMethodNamestringFor example stripe and Credit Card.
transactionsarrayPayment transactions: { transactionId?, type, amount, paymentAction, parentTransactionId?, additionalInformation?, createdAt? }. A transaction without transactionId is stored without one (several are allowed).
activityarrayExtra lines for the order's history: { comment, customerNotified?, createdAt? }.
metaDataobjectStored in the order's meta_data.

Each entry of items:

FieldTypeDescription
skustringRequired. The product is found by its sku and must exist in the store. Use productId to name the product by id instead.
namestringRequired. The name on the order (it need not match the product today).
qtyintegerRequired, above 0.
pricenumberRequired. The unit price without tax.
priceInclTax, taxPercentnumberThe 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.
discountAmountnumberThe discount of the whole line, without tax. Default 0, at most the line. It comes off before the tax is worked out.
weight, thumbnail, variantOptionsStored on the order item.
noShippingRequiredbooleanFor 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_created is not written. No e-mail is sent.
  • placed_at is set to createdAt, unless the payment status is pending, canceled or a failed status, no transaction is given and the payment method is not cod: such an order was never placed. A Cash on Delivery order is placed when it is created. order_placed is 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​