Search.../

onOrderCreated( )

An event that triggers when an order is created.

Description

The onOrderCreated() event handler runs when an order is created. The received OrderCreatedEvent object contains information about the created order.

This event handler runs for both online orders and offline orders.

Note: Backend events don't work when previewing your site.

Syntax

function onOrderCreated(event: OrderCreatedEvent): void

onOrderCreated Parameters

NAME
TYPE
DESCRIPTION
event
OrderCreatedEvent

Information about the created order and metadata for the event.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

An event triggered when an order is created

Copy Code
1// Place this code in the events.js file
2// of your site's Backend section.
3// Add the file if it doesn't exist.
4
5export function wixPricingPlans_onOrderCreated(event) {
6 const orderId = event.entity._id;
7 const eventId = event.metadata.id;
8 const eventTime = event.metadata.eventTime;
9}
10
11/* Full event object:
12 * {
13 * "metadata": {
14 * "id": "b879decf-c5b2-42e0-8066-03643691686b",
15 * "entityId": "d98900ac-a043-4930-b602-626dc00c4089",
16 * "eventTime": "2022-07-26T14:16:15.845649Z",
17 * "triggeredByAnonymizeRequest": false
18 * },
19 * "entity": {
20 * "_id": "d98900ac-a043-4930-b602-626dc00c4089",
21 * "planId": "099e2c86-3b7e-4477-8c27-f77402b8cceb",
22 * "subscriptionId": "3cc49b37-64e2-4b7f-aa87-e05e2729e41c",
23 * "wixPayOrderId": "d7941e1f-c7b9-40bb-a31c-2165fd582087",
24 * "buyer": {
25 * "memberId": "ea3d74df-b7dc-4ca1-a7c9-c416b9017a86",
26 * "contactId":"ea3d74df-b7dc-4ca1-a7c9-c416b9017a86"
27 * },
28 * "priceDetails": {
29 * "subtotal": "74.99",
30 * "discount": "0",
31 * "total": "74.99",
32 * "planPrice": "74.99",
33 * "currency": "EUR",
34 * "subscription": {
35 * "cycleDuration": {
36 * "count": 1,
37 * "unit":"MONTH"
38 * },
39 * "cycleCount": 3
40 * }},
41 * "pricing": {
42 * "subscription": {
43 * "cycleDuration": {
44 * "count": 1,
45 * "unit":"MONTH"
46 * },
47 * "cycleCount": 3
48 * },
49 * "prices": [{
50 * "duration": {
51 * "cycleFrom": 1,
52 * "numberOfCycles": 3
53 * },
54 * "price": {
55 * "subtotal": "74.99",
56 * "discount": "0",
57 * "total": "74.99",
58 * "currency": "EUR"
59 * }}]},
60 * "type": "OFFLINE",
61 * "orderMethod": "UNKNOWN",
62 * "status": "ACTIVE",
63 * "autoRenewCanceled": false,
64 * "lastPaymentStatus": "PAID",
65 * "startDate": "2022-07-26T14:16:15.194Z",
66 * "endDate": "2022-10-26T14:16:15.194Z",
67 * "pausePeriods": [],
68 * "earliestEndDate": "2022-10-26T14:16:15.194Z",
69 * "currentCycle": {
70 * "index": 1,
71 * "startedDate": "2022-07-26T14:16:15.194Z",
72 * "endedDate": "2022-08-26T14:16:15.194Z"
73 * },
74 * "planName": "Platinum Pro",
75 * "planDescription": "",
76 * "planPrice": "74.99",
77 * "_createdDate": "2022-07-26T14:16:15.194Z",
78 * "_updatedDate": "2022-07-26T14:16:15.194Z"
79 * }
80 * }
81 */
A full order plan scenario including a collection

This example demonstrates how to create an order and save its details in a collection after an event fires.

The code assumes:

  • An Orders collection with the following fields: _id, planId, planName, nickname, and status
  • The Members app is installed on the site, including the Members/PublicData collection
  • The Pricing Plans app is installed on the site, including the PaidPlans/Plans collection
  • A backend code file exists called process-orders.jsw
  • A backend events file exists called events.js

The OrderCreated event is fired in response to an order being created. The initial order creation might be initiated in several ways, such as by a button click on the page or by an external event request sent by an HTTP function call. The order creation is routed to backend code.

In this example, when the OrderCreated event is fired:

  • The myInsertOrderFunction() function is called, with the order object extracted from the event object
  • The myInsertOrderFunction() function calls a getMember() function to gets details about the member ordering the plan
  • The myInsertOrderFunction() function calls the Wix Data insert() function to add order and member details into the Orders collection

Copy Code
1/***********************************************
2 * For demonstration purposes, the *
3 * following myCreateOnlineOrderFunction() *
4 * triggers the onOrderCreated event. You *
5 * can put this code in a backend jsw file *
6 * or use a different method for triggering *
7 * the event such as with http functions. *
8 ***********************************************/
9
10import { checkout } from 'wix-pricing-plans-backend';
11
12export const myCreateOnlineOrderFunction = webMethod(Permissions.Anyone, (planId) => {
13 return checkout.createOnlineOrder(planId);
14});
15
16/*******************************
17 * Backend code - events.js *
18 *******************************/
19
20import { myInsertNewOrderFunction } from 'backend/process-orders.web';
21
22export function wixPricingPlans_onOrderCreated(event) {
23 myInsertNewOrderFunction(event.entity);
24}
25
26/****************************************
27 * Backend code - process-orders.web.js *
28 ***************************************/
29import { Permissions, webMethod } from 'wix-web-module';
30import wixData from 'wix-data';
31
32export const myInsertOrderFunction = webMethod(Permissions.Anyone, async (order) => {
33
34 const member = await getMember(order.buyer.memberId);
35 const data = ({
36 _id: order.id,
37 planId: order.planId,
38 planName: order.planName,
39 status: order.status,
40 nickname: member.nickname
41 });
42
43 wixData.insert('Orders', data)
44 .then(() => {
45 console.log('Inserted successfully', data);
46 })
47 .catch(() => {
48 console.error('Failed to insert', data);
49 });
50});
51
52// Gets member details to associate with the order
53async function getMember(memberId) {
54 try {
55 return wixData.get('Members/PublicData', memberId)
56 } catch (error) {
57 console.error('Failed to fetch member by ID', memberId, error);
58 };
59}