How can I get data-ms-content conditional logic working for plan-specific content and also when users lack a specific plan?

Post author
Eduardo Matos

Hey guys!

I'm trying to use the data-ms-content="!CONTENT-ID" to show a specific content only if the user doesn't have a specific plan, but it's simply not working. Thoughts?

Since this functionality is mentioned in this article I thought it was supposed to work smoothly.

Comments

4 comments

  • Comment author
    A J

    Hey Eduardo Matos, can you share your site preview link and mention which page you have enabled this feature to troubleshoot this in a better way?

    With the right attribute and relevant gated content ID, it should ideally work as you expect it to.

    0
  • Comment author
    Eduardo Matos

    A J It actually worked now, don't know why exactly lol

    Thanks anyway

    Btw, if I want to run some function based on a condition if user have plan X, is there any script already developed for this that I can use as a base?

    0
  • Comment author
    A J

    Welcome Eduardo Matos.

    Yes, I had saved a code shared by Chukwudi in the community earlier for a user who was trying to display the current member's plan name via code.

    <script>
    document.addEventListener("DOMContentLoaded", function () {
        const planNameElement = document.querySelector('#planName');
    
    window.$memberstackDom.getCurrentMember().then((member) => {
            if (member) {
                const planConnections = member.data["planConnections"];
    
                if (planConnections && planConnections.length > 0) {
                    const priceA = "prc_IDForPriceA"; // Replace with the Price ID of Price A
                    const priceB = "prc_IDForPriceB"; // Replace with the Price ID of Price B
                    const planA = "pln_IDForPlanA"; // Replace with the Plan ID of Plan A
                    const planB = "pln_IDForPlanB"; // Replace with the Plan ID of Plan B
    
                    // Filter out planConnections where payment or planId is null or undefined
                    const priceIds = planConnections
                        .filter(connection => connection.payment) // Ensure payment exists
                        .map(connection => connection.payment.priceId);
    
                    const planIds = planConnections
                        .filter(connection => connection.planId) // Ensure planId exists
                        .map(connection => connection.planId);
    
                    let planName = "No active plan or price"; // Default message
    
                    // Check for price IDs and set the plan name
                    if (priceIds.includes(priceA)) { planName = 'Monthly Plan'; }
                    if (priceIds.includes(priceB)) { planName = 'Annual Plan'; }
    
                    // Check for plan IDs and set the plan name
                    if (planIds.includes(planA)) { planName = 'Plan A'; }
                    if (planIds.includes(planB)) { planName = 'Plan B'; }
    
                    // Update the element with the plan name
                    planNameElement.textContent = planName;
    
                } else {
                    planNameElement.textContent = "No active plan or price";
                }
            }
        }).catch(() => {
            console.error('Failed to load Memberstack data.');
            planNameElement.textContent = "Unable to load plan details";
        });
    });
    </script>
    

    You can use this as a base code and customize it for your use-case accordingly. Hope this helps.

    0
  • Comment author
    Duncan from Memberstack

    Sometimes you have to refresh the page once or twice (the very first time you add them) for them to start working every time in the future. That may have been the issue here

    0

Please sign in to leave a comment.