Hello I have this code that copy order information to dtabase collection
However once data get stored the clients are identified as an ID …. Instead of names and email.
How can I collect the First name, Name, and email address ? In the fallowing code ?
Thanks you so much in advance.
Can you help me please.
/*****************************
- Backend code in events.js *
*****************************/
// For inserting data into a collection.
import wixData from ‘wix-data’;
// The onPlanPurchased() event is fired when a plan
// is purchased, or a free plan is ordered.
// Get the order information
// from the event’s order object.
export function wixPaidPlans_onPlanPurchased (event) {
// The PlanEvents collection has a title field,
// in which we insert the type of transaction.
// The collection also has a data field,
// where we will insert information about the order
// from the event's order object (json).
if (event.order.price.amount === 0) {
let orderData = {
“title”: “Free plan purchased”,
“data”: event.order
};
wixData.insert(“PlanEvents”, orderData);
} else {
let orderData = {
“title”: “Regular plan purchased”,
“data”: event.order
};
wixData.insert(“PlanEvents”, orderData);
}
}
It will always give you an ID only. Using that data from the event, you’d probably need to query the members database https://support.wix.com/en/article/velo-wix-members-privatemembersdata-collection-fields to retrieve that data and put it in your “PlanEvents” db.
Hello Jaosh,
Thanks for your answer.
I’m not a programmer.
Could you edit the code provided in the first post and add a case that will handle the email. From here i should be hable to add other informations myself based on your exemple.
Thanks a lot.
Massimo
Ok. Not too much of one myself. But you can try on the lines of this code.
export function wixPaidPlans_onPlanPurchased(event) {
// Insert a title reflecting the type of transaction, and
// the event's order object (json) into
// the collection's data field.
if (event.order.price.amount === 0) {
let planmemberid = event.order.memberId; //takes id of member from purchased plan
console.log(planmemberid); //just to check
wixData.query("Members/PrivateMembersData")
.eq("_id", planmemberid)
.find()
.then( (results) => {
if (results.items.length > 0) {
let items = results.items;
let item = items[0];
console.log(items); //just to check
let firstname = item.firstName;
let lastname = item.lastName;
let email = item.emails;
let orderData = {
"title": "Free plan purchased",
"first": firstname,
"last": lastname,
"email": email
};
wixData.insert("planEvents", orderData);
console.log(orderData);
console.log("data entered");
}
else {
console.log("nothing found");
}
});
}
else if (event.order.price.amount > 0) {
//same thing as above but for purchased plan
}
}
Hello,
Thanks a lot for your help.
How am i suppose to use your code :
- Overwrite mine ?
- Add it underneath ?
I tried both and it doesn’t work anymore, the “Data” column isn’t filled anymore with “OrderData Informations” and the “email”, “first” and “last” colum aren’t filled with excepected informations.
Bit lost here.
Massimo
I was hable to fix the code with a friend. It was just a typo, and two missing lines.
I attach it here fixed. In case someone else need it.
/*****************************
* Backend code in events.js *
*****************************/
// For inserting data into a collection.
import wixData from 'wix-data';
export function wixPaidPlans_onPlanPurchased(event) {
// Insert a title reflecting the type of transaction, and
// the event's order object (json) into
// the collection's data field.
if (event.order.price.amount === 0) {
let planmemberid = event.order.memberId; //takes id of member from purchased plan
console.log(planmemberid); //just to check
wixData.query("Members/PrivateMembersData")
.eq("_id", planmemberid)
.find()
.then( (results) => {
if (results.items.length > 0) {
let items = results.items;
let item = items[0];
console.log(items); //just to check
let firstname = item.firstName;
let lastname = item.lastName;
let email = item.emails;
let orderData = {
"title": "Free plan purchased",
"first": firstname,
"last": lastname,
"data": event.order,
"email": email
};
wixData.insert("PlanEvents", orderData);
console.log(orderData);
console.log("data entered");
}
else {
console.log("nothing found");
}
});
}
else if (event.order.price.amount > 0) {
//same thing as above but for purchased plan
}
}
Thanks a Million to @jaoshsethna It does work great !