Update the ID of the member in the CMS while registering for a course Answered

Post author
Gareth Klapproth

Hi team,

I've used Member stack in the past (v1). We have a client who wants to create an event management system and we are thinking of integrating MS with Airtable. My question is, if a user logs in, then goes to a course page and registers for a course (via form), will we be able to pull the current member ID of the current logged in user to then update the CMS in Webflow/Airtable?

Help much appreciated!

Comments

4 comments

  • Comment author
    Chris Drit

    You can get the Memberstack member id for any logged in user with Webflow by doing this:

    <script>
    window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
      if (member) {
        // access email using: member.auth.email
        // access id using: member.id
        // access custom fields using: member.customFields["your_field_name"]
        console.log(member)
      } else {
        console.log("not logged in")
      }
    })
    </script>

    That’s not the hard part.

    The difficult aspect is updating the Webflow CMS.

    The only way you have access (outside of Webflow Logic) to update a CMS item is through the item id. You can’t search for things, or access the item through a different field.

    So the general approach is to track that item id after you create the CMS item in a lookup table.

    Here’s a tutorial on how I’ve used Lookup Tables in the past to help with this.

    There are other approaches too. Like storing the Webflow item ID in the Memberstack metadata for that logged in user:

    <script>
    // Get current member's JSON 
    let memberJson = await window.$memberstackDom.getMemberJSON();
    
    // Modify or add new data 
    memberJson.name = "Chris Drit";
    memberJson.itemID = "ed5trg3eds4rgfr";
    
    // Update member's JSON 
    await window.$memberstackDom.updateMemberJSON({json: memberJson});
    </script>

    Hope that helps!

    0
  • Comment author
    Gareth Klapproth

    Yes this helps. The idea is to pass course registration to a CRM / Airtable without them having to put their email in again. I.e a register button

    0
  • Comment author
    Ben Sabic

    Hey Gareth Klapproth!

    Something like this should help you get the job done.

    If you add this snippet to the course page, the member's ID will be automatically added to every form. A hidden input is added to the form, which stores the member ID. So when the form is submitted, you'll know which member submitted it; or in this case, which member signed up for the course.

    window.$memberstackDom.getCurrentMember().then(({ data: member }) => {   
    
        if (member) {
    
          let memberID = member.id
    
          if (memberID) {
          
          const memberIDinput = document.createElement('input');
          memberIDinput.type = 'hidden';
          memberIDinput.name = 'Member ID';
          memberIDinput.value = memberID;
    
          const forms = document.getElementsByTagName('form');
    
            for (const form of forms) { form.appendChild(memberIDinput.cloneNode(true)); }
    
        }
    
    }
    
    })
    0
  • Comment author
    Julian Galluzzo

    Here's an even easier, totally no code method!

    1. Create an input in webflow
    2. Set the display to hidden
    3. Add the attribute data-ms-member="id"

    And their ID will pass through in the form!

    0

Please sign in to leave a comment.