Get Member Login Email after Plan Purchase

Hi there,

This is my first time posting on this forum, and I have tried very hard to avoid it, because I do believe it is best to spend the time to go through the references and learn it yourself if possible. But on this issue, I’ve gotten kind of stumped. I’m relatively new to code, and am currently learning Javascript to help me better understand some of the things going on in Wix Code. I do feel I’ve got a pretty good handle on it now, but I’m stuck right here.

Okay here’s what I’m trying to do. I’ve set up a custom database for when people purchase a paid plan on my site, and I’ve set the code to assign them a Member Role after purchasing, which gives them access to a specific page on the site where they can use an online tool I’ve created. So far, that part of the code works fine. But I also want to insert the Member’s Login Email as a field in my custom database, so I can filter their purchases on another page. I can’t for the life of me figure out how to get their email and include it in the “onPlanPurchased” event which inserts the data.

(side note: I can’t use “Owner is Logged-In User” because the owner for every entry is always me, even when it’s someone else logged in. I assume this is because the data entry is not from the user, but from my code on the backend. So instead I’m thinking I’ll filter by email, matched to the “private-member-data” collection.)

Below I’ve pasted what my code currently looks like, please let me know what I need to do to get the email of the current user , and include it in this database entry when they purchase a plan. For this example, the database I’m adding data to here is called “PaidPlans-ManualDataRetrieval”.

Any help or insight is greatly appreciated!

-Jared

import wixData from ‘wix-data’ ;
import { authorization } from ‘wix-members-backend’ ;

export function wixPaidPlans_onPlanPurchased ( event ) {

 **let**  orderData  = { 
   "title" :  event . order . planName , 
   "data" :  event . order , 
   "memberId" :  event . order . memberId , 
 }; 
 wixData . insert ( "PaidPlans-ManualDataRetrieval" ,  orderData );  

const roleId = “6c3b83ea-04e8-4470-9e06-8861b6d8c2dd” ;
const memberId = event . order . memberId ;
const options = {
suppressAuth : false
};

return authorization . assignRole ( roleId , memberId , options )
. then (() => {
console . log ( “Role assigned to member” );
})
. catch (( error ) => {
console . error ( error );
});
}

Next time make sure, you open a question → NOT <— a discussion.

Already have seen this one…

…and this one …?

Also use CODE-BLOCKS → when you show your code, like this…

import wixData from 'wix-data';
import { authorization } from 'wix-members-backend';


export function wixPaidPlans_onPlanPurchased(event) {

     let orderData = {
       "title": event.order.planName,
       "data": event.order,
       "memberId": event.order.memberId,
     };
     wixData.insert("PaidPlans-ManualDataRetrieval", orderData); 

  const roleId = "6c3b83ea-04e8-4470-9e06-8861b6d8c2dd";
  const memberId = event.order.memberId;
  const options = {
    suppressAuth: false
  };

  return authorization.assignRole(roleId, memberId, options)
    .then(() => {
      console.log("Role assigned to member");
    })
    .catch((error) => {
      console.error(error);
    });
}

This type of CODE display will save us all from eye cancer :grin:

Try this code and let me know. Haven’t had the time to test it.

import wixData from 'wix-data'
import { authorization } from 'wix-members-backend'
import { contacts } from 'wix-crm-backend' //Import contacts method from 'wix-crm-backend'

let options = {
    suppressAuth: false,
    suppressHooks: false,
}

export async function wixPaidPlans_onPlanPurchased(event) {
    const { memberId } = event
    const queryMember = await contacts.queryContacts().eq('_id', id).find(options)
    const {
        primaryInfo: { email },
    } = queryMember.items[0]

    let orderData = {
        title: 'test',
        data: 'test',
        memberId: 'test',
        email, //Member Email
    }

    wixData.insert('PaidPlans-ManualDataRetrieval', orderData)

    const roleId = '6c3b83ea-04e8-4470-9e06-8861b6d8c2dd'
    
    return authorization
        .assignRole(roleId, memberId, options)
        .then(() => {
            console.log('Role assigned to member')
        })
        .catch(error => {
            console.error(error)
        })
}
1 Like

Okay. Sorry I didn’t see the option to make it a question before, just create post. I now see it’s a dropdown option. I will format better next time. And I did see posts about the wixUsers backend, but I thought it was deprecated and not recommended anymore, but I will check out those links you sent and see if it helps. Thank you for the suggestions.

And thank you Bruno! I had to step out for a bit but will check this tonight and get back to you!

1 Like

Okay so this worked with a bit of adjustment!

Here’s what the final code looks like. Now when a plan is purchased, I have a custom database that collects 1) the Plan Name, 2) the full JSON data of the purchase (just because I like having as much info as possible), 3) the Member Id, and 4) the email of the purchaser.

And it then assigns the user to a temporary Member Role that gives them access to my online tool page (and the role will be removed after they use the tool once, as it is a one-time use purchase each time). Thank you very much for the help here!

import wixData from 'wix-data'
import { authorization } from 'wix-members-backend'
import { contacts } from 'wix-crm-backend' //Import contacts method from 'wix-crm-backend'

let options = {
    suppressAuth: false,
    suppressHooks: false,
}

export async function wixPaidPlans_onPlanPurchased(event) {
    const { memberId } = event
    const queryMember = await contacts.queryContacts().eq('_id', memberId).find(options)
    const {
        primaryInfo: { email },
    } = queryMember.items[0]

    let orderData = {
        title: event.order.planName,
        data: event.order,
        memberId: event.order.memberId,
        loginEmail: email
    }

    wixData.insert('PaidPlans-ManualDataRetrieval', orderData)

    const roleId = '6c3b83ea-04e8-4470-9e06-8861b6d8c2dd'
    
    return authorization
        .assignRole(roleId, memberId, options)
        .then(() => {
            console.log('Role assigned to member')
        })
        .catch(error => {
            console.error(error)
        })
}


I was trying forever to get something like this to work with the wix-members-backend, but could not for the life of me get it to do this. So I suppose the wix-crm-backend is my answer at the moment. Out of curiosity, do you know how this could be achieved using wix-members-backend instead? Just curious if what I was attempting to do was on the right track.

Thank you for all the help!

2 Likes

Actually, you know what, I just realized that this still doesn’t quite fix it. I realized it just fills in my personal email address every time (the email address I used to sign up to Wix), I have several test accounts on my own site, all with different email addresses, and none of them match my Wix email address. And I’ve tested this on all of my test accounts now and it always fills the “loginEmail” field with my Six account email address.

UPDATE okay, I seem to have solved it. It needed “event.order.memberId” in the query.

I went from this:

 export async function wixPaidPlans_onPlanPurchased(event) {
    const { memberId } = event
    const queryMember = await contacts.queryContacts().eq('_id', memberId).find(options)

To this:

 export async function wixPaidPlans_onPlanPurchased(event) {
    const { memberId } = event
    const queryMember = await contacts.queryContacts().eq('_id', event.order.memberId).find(options)

Seems to be working good now.

Sorry about that mistake, I messed up the memberId destructuring.

It should be this:

const { order: { memberId } } = event
1 Like