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

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

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.

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
This is called from the server and thus will not involve any unnecessary validation process

1 Like

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

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:

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

Hope This Helps

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
}

}