0

I am trying to use Wix Velo to save additional data when a user purchases a subscription plan. They have forms built in for that, but I can't then access that data later on in my code. So I'm trying to do it using a custom Collection.

I am doing this since my subscriptions are by state, so a single user can sign up for the same subscription in multiple states but I need to be able to track what state each subscription is for.

I first use the promise to start the online purchase, and using the orderObject that is returned, along with the additional data I am trying to collect, I push it to a backend function that creates a new line in the collection with the purchase data and additional data. Code is below:

 function processPlan(planId, state) { checkout.startOnlinePurchase(planId) .then((orderObject) => { addStateToPlanEvent(orderObject, state, userId); $w('#multiStateBox1').changeState("confirmSignUp"); }) .catch((error) => { console.error("Error: ", error.message); }); } 

backend function:

export function addStateToPlanEvent(event, state, userId) { if(event.order.price.amount === 0){ let orderData = { "title": "Free Plan Purchased", "data": event.order, "orderId": event.order.orderId, "stateSubscribed": state, "memberId": userId } console.log(orderData); wixData.insert("PlanEvents", orderData); }else{ let orderData = { "title": "Plan Purchased", "data": event.order, "orderId": event.order.orderId, "stateSubscribed": state, "memberId": userId } console.log(orderData); wixData.insert("PlanEvents", orderData); } } 

unfortunately, my code seems to skip over the backend function without running it. No lines are added to my collection, but my code doesn't give an error, and it does complete the next line of the function processPlan that changes the multi-state box to confirm the signup.

I am not too familiar with promises, could it be that having a regular function inside an async function inside a regular function is causing the issue? I don't know how else to link the state variable to the order once I step out of the promise. Is there a better way I'm missing?

any help would be appreciated!

2
  • So you don't see the output of console.log(orderData)? Commented Sep 7, 2023 at 17:53
  • Turns out it was a syntax error, I wasn't aware that the console logs don't appear in dev tools, Wix has their own dev tools logs that gave me the error so I was able to fix it! Commented Sep 8, 2023 at 16:55

1 Answer 1

0

Once I realized that Wix has their own Dev Tools where console logs appear instead of Google Dev tools, I found the syntax errors and then the code worked as it should! The event.content was not laid out properly, apparently, the Wix documentation I got the code from is a little outdated. Corrections below:

event.order.orderId ==> event.order._id event.order.price.amount ==> event.order.planPrice event.order ==> event 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.