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
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
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.
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âŚ
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
}