Search...
createOrder( )
Developer Preview
Creates an order from a specified checkout.
Description
The createOrder()
function returns a Promise that resolves to the new order's ID and paymentGatewayOrderID
when the order is created.
Pass the paymentGatewayOrderId
as the paymentId
param to the startPayment()
function to allow a customer to pay for their order.
Note: The following requirements must be met for an order to be created from a checkout.
- A checkout cannot have calculation errors. Pass the
checkout._id
to Get Checkout and take a look at thecalculationErrors
field.- A checkout must have at least 1 line item.
- All of the line Items have an
availability.status
of"AVAILABLE"
or"PARTIALLY_AVAILABLE"
.- If there is a payment to be made, meaning that
priceSummary.total
is greater than 0, thebillingInfo.address
field must be provided.- When a checkout has line items to be shipped, the
shippingInfo.shippingDestination.address
andshippingInfo.selectedCarrierServiceOption
fields must be provided.- When a checkout has line items for pickup, the
shippingInfo.selectedCarrierServiceOption.logistics.pickupDetails
field must be provided.
Syntax
function createOrder(_id: string): Promise<CreateOrderResponse>
createOrder Parameters
NAME
TYPE
DESCRIPTION
_id
string
Checkout ID.
Returns
Return Type:
Promise<
CreateOrderResponse
>NAME
TYPE
DESCRIPTION
orderId
string
ID of the newly created order.
paymentGatewayOrderId
string
Payment gateway order ID.
For online orders, pass this value as the paymentId
parameter to the Wix Pay startPayment()
function so your customer can pay for the order.
This field will be returned if money needs to be charged. In some cases, money cannot be charged:
- When the total price (the
priceSummary.total.amount
field in the checkout/order objects) is 0. For example, in the case of a free item or an item with a 100% discount. - If the total price is not 0, but the payment is covered by alternative payment methods, such as a gift card.
Was this helpful?
Create an order from a checkout
Copy Code
1/**************************************2 * Backend code - my-backend-file.jsw *3 *************************************/45import { checkout } from 'wix-ecom-backend';67export async function myCreateOrderFromCheckoutFunction(checkoutId) {8 try {9 const createOrderResponse = await checkout.createOrder(checkoutId);10 console.log('Success! Created an order from the checkout');11 return createOrderResponse;12 } catch (error) {13 console.error(error);14 // Handle the error15 }16}1718/*************19 * Page code *20 ************/2122import { myCreateOrderFromCheckoutFunction } from 'backend/my-backend-file';2324// Sample checkoutId:25const checkoutId = '23a7b29a-3c14-4ef1-9353-f4b714b13217';2627myCreateOrderFromCheckoutFunction(checkoutId)28 .then((createOrderResponse) => {29 const orderId = createOrderResponse.orderId;30 const paymentGatewayOrderId = createOrderResponse.paymentGatewayOrderId;3132 console.log('Success! Created an order from the checkout');33 return createOrderResponse;34 })35 .catch((error) => {36 console.error(error);37 // Handle the error38 });3940/* Promise resolves to:41 *42 * {43 * "orderId": "02843248-495f-45c0-a5d6-e913f647c9f2",44 * "paymentGatewayOrderId": "f30440f4-413a-4382-bc38-7280b155a5ed"45 * }46 *47 */
Create an order from a checkout (export from backend code)
Copy Code
1/**************************************2 * Backend code - my-backend-file.web.js *3 *************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { checkout } from 'wix-ecom-backend';78export const myCreateOrderFromCheckoutFunction = webMethod(Permissions.Anyone, async (checkoutId) => {9 try {10 const createOrderResponse = await checkout.createOrder(checkoutId);11 console.log('Success! Created an order from the checkout');12 return createOrderResponse;13 } catch (error) {14 console.error(error);15 // Handle the error16 }17});1819/*************20 * Page code *21 ************/2223import { myCreateOrderFromCheckoutFunction } from 'backend/my-backend-file.web';2425// Sample checkoutId:26const checkoutId = '23a7b29a-3c14-4ef1-9353-f4b714b13217';2728myCreateOrderFromCheckoutFunction(checkoutId)29 .then((createOrderResponse) => {30 const orderId = createOrderResponse.orderId;31 const paymentGatewayOrderId = createOrderResponse.paymentGatewayOrderId;3233 console.log('Success! Created an order from the checkout');34 return createOrderResponse;35 })36 .catch((error) => {37 console.error(error);38 // Handle the error39 });4041/* Promise resolves to:42 *43 * {44 * "orderId": "02843248-495f-45c0-a5d6-e913f647c9f2",45 * "paymentGatewayOrderId": "f30440f4-413a-4382-bc38-7280b155a5ed"46 * }47 *48 */