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
Information about the created order and metadata for the event.
Returns
This function does not return anything.
Return Type:
Was this helpful?
1// Place this code in the events.js file2// of your site's Backend section.3// Add the file if it doesn't exist.45export function wixPricingPlans_onOrderCreated(event) {6 const orderId = event.entity._id;7 const eventId = event.metadata.id;8 const eventTime = event.metadata.eventTime;9}1011/* 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": false18 * },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": 340 * }},41 * "pricing": {42 * "subscription": {43 * "cycleDuration": {44 * "count": 1,45 * "unit":"MONTH"46 * },47 * "cycleCount": 348 * },49 * "prices": [{50 * "duration": {51 * "cycleFrom": 1,52 * "numberOfCycles": 353 * },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 */
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
, andstatus
- The
Members
app is installed on the site, including theMembers/PublicData
collection - The
Pricing Plans
app is installed on the site, including thePaidPlans/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 theorder
object extracted from the event object - The
myInsertOrderFunction()
function calls agetMember()
function to gets details about the member ordering the plan - The
myInsertOrderFunction()
function calls the Wix Datainsert()
function to add order and member details into theOrders
collection
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 ***********************************************/910import { checkout } from 'wix-pricing-plans-backend';1112export function myCreateOnlineOrderFunction(planId) {13 return checkout.createOnlineOrder(planId);14}1516/*******************************17 * Backend code - events.js *18 *******************************/1920 import { myInsertNewOrderFunction } from 'backend/process-orders'2122 export function wixPricingPlans_onOrderCreated(event) {23 myInsertNewOrderFunction(event.entity);24}2526/*************************************27 * Backend code - process-orders.jsw *28 *************************************/29 import wixData from 'wix-data';3031 export async function myInsertOrderFunction(order) {3233 const member = await getMember(order.buyer.memberId);34 const data = ({35 _id: order.id,36 planId: order.planId,37 planName: order.planName,38 status: order.status,39 nickname: member.nickname40 });4142 wixData.insert('Orders', data)43 .then(() => {44 console.log('Inserted successfully', data);45 })46 .catch(() => {47 console.error('Failed to insert', data);48 });49 }5051 // Gets member details to associate with the order52 async function getMember(memberId) {53 try {54 return wixData.get('Members/PublicData', memberId)55 } catch (error) {56 console.error('Failed to fetch member by ID', memberId, error);57 };58 }