> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: onOrderCreated(event: OrderCreatedEvent)

# Method package: wixPricingPlansBackend

# Method menu location: wixPricingPlansBackend --> Events --> onOrderCreated

# Method Link: https://dev.wix.com/docs/velo/apis/wix-pricing-plans-backend/events/on-order-created.md

# Method Description:

An event that triggers when an order is created.

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.



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## An event triggered when an order is created

```javascript
// Place this code in the events.js file
// of your site's Backend section.
// Add the file if it doesn't exist.

export function wixPricingPlans_onOrderCreated(event) {
  const orderId = event.entity._id;
  const eventId = event.metadata.id;
  const eventTime = event.metadata.eventTime;
}

/* Full event object:
 * {
 *   "metadata": {
 *     "id": "b879decf-c5b2-42e0-8066-03643691686b",
 *     "entityId": "d98900ac-a043-4930-b602-626dc00c4089",
 *     "eventTime": "2022-07-26T14:16:15.845649Z",
 *     "triggeredByAnonymizeRequest": false
 *   },
 *   "entity": {
 *     "_id": "d98900ac-a043-4930-b602-626dc00c4089",
 *     "planId": "099e2c86-3b7e-4477-8c27-f77402b8cceb",
 *     "subscriptionId": "3cc49b37-64e2-4b7f-aa87-e05e2729e41c",
 *     "wixPayOrderId": "d7941e1f-c7b9-40bb-a31c-2165fd582087",
 *     "buyer": {
 *       "memberId": "ea3d74df-b7dc-4ca1-a7c9-c416b9017a86", 
 *       "contactId":"ea3d74df-b7dc-4ca1-a7c9-c416b9017a86" 
 *     },
 *     "priceDetails": {
 *       "subtotal": "74.99",
 *       "discount": "0",
 *       "total": "74.99",
 *       "planPrice": "74.99",
 *       "currency": "EUR",
 *       "subscription": {
 *         "cycleDuration": {
 *           "count": 1,
 *           "unit":"MONTH" 
 *         },
 *         "cycleCount": 3 
 *       }},
 *     "pricing": {
 *       "subscription": {
 *         "cycleDuration": {
 *           "count": 1,
 *           "unit":"MONTH"
 *         },
 *         "cycleCount": 3
 *       },
 *       "prices": [{
 *         "duration": {
 *           "cycleFrom": 1,
 *           "numberOfCycles": 3
 *         },
 *         "price": {
 *           "subtotal": "74.99",
 *           "discount": "0",
 *           "total": "74.99",
 *           "currency": "EUR"
 *         }}]},
 *     "type": "OFFLINE",
 *     "orderMethod": "UNKNOWN",
 *     "status": "ACTIVE",
 *     "autoRenewCanceled": false,
 *     "lastPaymentStatus": "PAID",
 *     "startDate": "2022-07-26T14:16:15.194Z",
 *     "endDate": "2022-10-26T14:16:15.194Z",
 *     "pausePeriods": [],
 *     "earliestEndDate": "2022-10-26T14:16:15.194Z",
 *     "currentCycle": {
 *       "index": 1,
 *       "startedDate": "2022-07-26T14:16:15.194Z",
 *       "endedDate": "2022-08-26T14:16:15.194Z"
 *     },
 *     "planName": "Platinum Pro",
 *     "planDescription": "",
 *     "planPrice": "74.99",
 *     "_createdDate": "2022-07-26T14:16:15.194Z",
 *     "_updatedDate": "2022-07-26T14:16:15.194Z"
 *   }
 * }
 */
```

## A full order plan scenario including a collection

```javascript
/***********************************************
 * For demonstration purposes, the             * 
 * following myCreateOnlineOrderFunction()     * 
 * triggers the onOrderCreated event. You      *
 * can put this code in a backend jsw file     *
 * or use a different method for triggering    *
 * the event such as with http functions.      *
 ***********************************************/

import { checkout } from 'wix-pricing-plans-backend';

export const myCreateOnlineOrderFunction = webMethod(Permissions.Anyone, (planId) => {
  return checkout.createOnlineOrder(planId);
});

/*******************************
 * Backend code - events.js    *
 *******************************/

import { myInsertNewOrderFunction } from 'backend/process-orders.web';

export function wixPricingPlans_onOrderCreated(event) {
  myInsertNewOrderFunction(event.entity);
}

/****************************************
 * Backend code - process-orders.web.js *
 ***************************************/
import { Permissions, webMethod } from 'wix-web-module';
import wixData from 'wix-data';

export const myInsertOrderFunction = webMethod(Permissions.Anyone, async (order) => {

    const member = await getMember(order.buyer.memberId);
    const data = ({
        _id: order.id,
        planId: order.planId,
        planName: order.planName,
        status: order.status,
        nickname: member.nickname
    });

    wixData.insert('Orders', data)
      .then(() => {
         console.log('Inserted successfully', data);
      })
      .catch(() => {
         console.error('Failed to insert', data);
      });
});

// Gets member details to associate with the order 
async function getMember(memberId) {
 try {
   return wixData.get('Members/PublicData', memberId)
 } catch (error) {
   console.error('Failed to fetch member by ID', memberId, error);
 };
}
```

---