# Gather order ID and number and add it to CMS collection?

**URL:** <https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415>\
**Category:** Ask the community\
**Tags:** technical-talk, code, wix, wix-stores, question\
**Created:** [December 6, 2024, 11:44am UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415 "2024-12-06T11:44:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Kay](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@M\_Kay](https://forum.wixstudio.com/u/M_Kay)\
**Post date:** [December 6, 2024, 11:44am UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/1 "2024-12-06T11:44:21Z")

</div>

Hi there,  
I want to fetch the following information from the thank you page and add it to a collection:

- orderId
- order number
- orderDate (timestamp)

Wix website editor

Users accumulate data on my website that needs to be connected to their order number after the checkout process, so I need to get that information to complete my dataset

---

<div class="post-metadata">

**Author:** ![Tech\_Voyager](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/tech_voyager/32/97093_2.png) [@Tech\_Voyager](https://forum.wixstudio.com/u/Tech_Voyager)\
**Post date:** [December 6, 2024, 12:17pm UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/2 "2024-12-06T12:17:51Z")

</div>

To fetch order details on the **thank you page** in Wix and store them in a collection, you can use Wix Velo’s backend capabilities and custom code.

---

<div class="post-metadata">

**Author:** ![DeanAyalon](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/deanayalon/32/94633_2.png) [@DeanAyalon](https://forum.wixstudio.com/u/DeanAyalon)\
**Post date:** [December 6, 2024, 1:38pm UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/3 "2024-12-06T13:38:54Z")

</div>

I would **NOT** add the CMS records from the ‘thank you’ page, as this would:  
A. Require the client to be browsing the site, rather than work for all (even offline) orders  
b. Require code to be called from the client side, which is a permission issue, anyone could call it at any time, and you’d need to implement quite a bit of validation

Instead, just have the CMS record be created upon order confirmation - [backend/events.js - wixEvents\_onOrderConfirmed](https://dev.wix.com/docs/velo/api-reference/wix-events-backend/events/on-order-confirmed)  
This is called from the server and thus will not involve any unnecessary validation process

---

<div class="post-metadata">

**Author:** ![M\_Kay](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@M\_Kay](https://forum.wixstudio.com/u/M_Kay)\
**Post date:** [December 8, 2024, 9:39am UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/4 "2024-12-08T09:39:35Z")

</div>

Is there a space here where I can get help with creating that code?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Christopher\_Fleming](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/christopher_fleming/32/101311_2.png) [@Christopher\_Fleming](https://forum.wixstudio.com/u/Christopher_Fleming)\
**Post date:** [December 8, 2024, 11:42am UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/5 "2024-12-08T11:42:32Z")

</div>

You will need to modify the javascript of your site to send a post to your own database. That seems like the simplest method from my perspective. As mentioned before, you wouldn’t get it from the customer(thank you page), you should get it from the backend of your site.

You will need to go here and read some of this documentation from wix:

> **[About Velo by Wix](https://dev.wix.com/docs/develop-websites/articles/getting-started/about-velo-by-wix)**
>
> Velo is an open development platform that accelerates the way you build web applications. Work in Wix's visual builder, add custom functionality and interactions using Velo's APIs, use your own tools, and enjoy serverless coding in both the front-end...

then also do some reading on javascript database queries and posts.

Hope This Helps

---

<div class="post-metadata">

**Author:** ![M\_Kay](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@M\_Kay](https://forum.wixstudio.com/u/M_Kay)\
**Post date:** [December 8, 2024, 12:14pm UTC](https://forum.wixstudio.com/t/gather-order-id-and-number-and-add-it-to-cms-collection/65415/6 "2024-12-08T12:14:44Z")

</div>

As I said, I’m not big on coding, but this is what ChatGPT came up with. Doesn’t seem to work though. Can someone with more knowledge in the matter look over it and help me debug it? I really don’t know how to get this done otherwise…

import wixData from ‘wix-data’;

export async function orderAutomation(event) {  
try {  
console.log(“Received event object:”, JSON.stringify(event));

```
    const order = event.payload;
    if (!order.cartId || !order.orderNumber) {
        console.error("Missing cartId or orderNumber in the order object.");
        throw new Error("Missing cartId or orderNumber in the order object.");
    }

    const cartId = order.cartId;
    const orderNumber = order.orderNumber;

    console.log("Looking for a matching sticker with cartId:", cartId);

    // Query the StickerDesigns collection for the matching cartId
    const queryResult = await wixData.query("StickerDesigns")
        .eq("cartId", cartId)
        .find({ suppressAuth: true }); // Correct usage of suppressAuth

    if (queryResult.items.length === 0) {
        console.error("No matching StickerDesigns found for cartId:", cartId);
        throw new Error("No matching StickerDesigns found for cartId.");
    }

    const sticker = queryResult.items[0];
    console.log("Matching sticker found:", sticker);

    // Update the sticker with order details
    sticker.orderNumberAutomation = orderNumber;
    sticker.orderDate = new Date();
    sticker.usedInOrder = true;
    sticker.orderId = order._id;

    await wixData.update("StickerDesigns", sticker, { suppressAuth: true }); // Correct usage of suppressAuth

    console.log(`StickerDesigns record updated successfully with order details for cartId: ${cartId}`);
} catch (error) {
    console.error("Error in orderAutomation:", error);
    throw error; // Re-throw the error to log it in the automation process
}

```

}
