How to Create Conditional Redirects for Plan-Specific Pages Answered

Post author
Heather B

Hello, please how can I redirect members on a specific plan (e.g. plan A) to the homepage if they land on the 'upgrade to plan A' page? I think the solution would be adding code similar to this but involving plan ids. Unfortunately this code from Rey did not work. Thanks

Comments

3 comments

  • Comment author
    A J

    Hey Heather B, just wondering if you have tried the Gated content settings instead for this. Say you gate the upgrade page, and allow access to this page only if the user is in a free plan?

    0
  • Comment author
    Chukwudi Onyekwere

    The code below is the right code snippet to achieve that. Do well to replace the planIDs and redirect URLs where necessary.

    <script>
    window.$memberstackDom.getCurrentMember().then((member) => {
    if (member.data) {
    const planConnections = member.data["planConnections"];
    if (planConnections && planConnections.length > 0) {
    const planA = "pln_IDForPlanA"; // Replace with the Plan ID of Plan A
    const planB = "pln_IDForPlanB"; // Replace with the Plan ID of Plan B
    const planIds = planConnections.map(connection => connection.planId);
    if (planIds.includes(planA)) {
    window.location.href = "https://example.com/dashboardA";
    } else if (planIds.includes(planB)) {
    window.location.href = "https://example.com/dashboardB";
    } else {
    window.location.href = "https://example.com/pricing";
    }
    }
    }
    });
    </script>
    0
  • Comment author
    Heather B

    A J thank you for the suggestion. Unfortunately it wasn't right for my use case I didn't want to gate the page from everyone not on plan B - i wanted people signed out to see the page too (and the gating method was redirecting them)

    Chukwudi Onyekwere thank you for your help! I tweaked the code you provided slightly for the reason mentioned above and it works perfect! I'll leave it below in case anyone might want it.

    thank you both again

    <script>
    window.$memberstackDom.getCurrentMember().then((member) => {
    if (member.data) {
    const planConnections = member.data["planConnections"];
    if (planConnections && planConnections.length > 0) {
    const planA = "pln_IDforplanA"; // Replace with the Plan ID of Plan A
    const memberPlanIds = planConnections.map(connection => connection.planId);
    if (memberPlanIds.includes(planA)) {
    window.location.href = "https://www.google.com"; // Redirect to any site you want e.g. Google for those already on Plan A
    }
    }
    }
    });
    </script>
    0

Please sign in to leave a comment.