How to automatically assign a free plan to users who abandon checkout in Memberstack with Stripe? Answered

In memberstack, I have paid plans as well as a free plan I've set up. I've set up the pricing table to use all the correct attributes and the flows all work if users complete the checkout in stripe.

I'm finding that in Memberstack any user that drops out of checkout just has no plan assigned to them, ideally I'd like users to have the free plan automatically applied to avoid this issue.

I tried adding the data-ms-plan:add attribute to the paid plan buttons where I've also got the data-ms-price:update attribute but that doesn't seem to achieve the aim.

How can I resolve this issue?

Comments

1 comment

  • Comment author
    Chukwudi
    • Official comment

    Hi Benjamin,

    You can use an automation tool like Make or Zapier to automatically add the free plan if the user doesn't complete the checkout process.

    Alternatively, you can place the following code just before the </body> of the page they land on after logging in (e.g., the dashboard). This code will first check if the user already has the free plan, and if not, it will add it automatically.

    Please make sure to replace 'YOUR_FREE_PLAN_ID' with your actual free plan ID:

    <script>
    document.addEventListener("DOMContentLoaded", function() {
        // Ensure Memberstack is defined and ready
        if (window.$memberstackDom) {
            window.$memberstackDom.getCurrentMember().then(async ({ data: member }) => {
                if (member) {
                    console.log(member);
                    
                    // Get plan connections and define the free plan ID
                  const planConnections = member["planConnections"];
                    const freePlanId = "YOUR_FREE_PLAN_ID"; // Replace with your actual Free Plan ID
                    let hasPaidPlan = false;
                    let hasFreePlan = false;

                    // Loop through the plan connections to check if the user has a paid plan or free plan
                    if (planConnections && planConnections.length > 0) {
                        for (let connection of planConnections) {
                            // Check if the user has a paid plan
                            if (connection.type !== 'FREE') {
                                hasPaidPlan = true;
                            }
                            // Check if the user already has the free plan
                            if (connection.planId === freePlanId) {
                                hasFreePlan = true;
                            }
                        }
                    }

                    // If the user doesn't have a paid plan and doesn't have the free plan, add the free plan
                    if (!hasPaidPlan && !hasFreePlan) {
                        try {
                            await window.$memberstackDom.addPlan({
                                planId: freePlanId
                            });
                            console.log("Free plan added successfully.");
                        } catch (error) {
                            console.error("Error adding free plan:", error);
                        }
                    } else if (hasPaidPlan) {
                        console.log("User already has a paid plan. No action needed.");
                    } else {
                        console.log("User already has the free plan.");
                    }
                }
            });
        } else {
            console.error("Memberstack is not defined. Please make sure the Memberstack script is properly loaded.");
        }
    });
    </script>

Please sign in to leave a comment.