Redirect user based on subscription status

Question:
How to redirect a user based on their subscription status?

Product:
Wix Editor on the Core premium plan

What are you trying to achieve:
I have a page which leads the user to the subscription page. I want to check, if the user has already subscribed, then they should be redirected to another page. The data being used is from Wix default data storage and there are no data collections created.

What have you already tried:
Different codes to make it work.
Forum Topic: Help Needed: Redirecting Customers to Specific Pages Based on Subscription Plan in Wix Code

Additional information:
I have the Core premium plan activated for my website. Is this the reason that the code does not work?

You can do that with Velo.

Thank you, Pratham.
It would be great, if you could guide me, how can I do that using velo?

Sure.

For users who have subscribed to your plans, do you want all the subscribers to go to the same page? Or do you want to redirect users based to specific pages based on the plans they have purchased, for example -

  • a person with a Gold Plan should be redirected to www.yoursite.com/gold-page
  • a person with a Platinum Plan should be redirected to www.yoursite.com/platinum-page

Thanks!
Yes, I want the users to go to 3 different pages, based on their subscription.

Okay, first go to the above page in the editor.

Then turn on Dev Mode from the toolbar at the top. You will see a code panel open up at the bottom.

Replace the code in the panel with the one I’ve provided below.

Make sure to rename plan1, plan 2 and plan3 and also their corresponding redirect URLs, i.e. redirect1, redirect2 and redirect3.
So lets say if the user has subscribed to the Gold Plan, the variables should be:

const plan1 = "Gold Plan";
const redirect1 = "https://yoursite.com/gold-page";

Repeat this for the other two plans.

Just a couple of things to make sure:

  • Set the access of this particular page to be Members Only, else it will lead to an error if the user is not logged in.
  • Make sure there are no red lines in the code.

Here’s the code:

import { orders } from 'wix-pricing-plans-frontend';
import wixLocationFrontend from 'wix-location-frontend';

const plan1 = "Enter plan name here";
const redirect1 = "Enter URL link here";

const plan2 = "Enter plan name here";
const redirect2 = "Enter URL link here";

const plan3 = "Enter plan name here";
const redirect3 = "Enter URL link here";

$w.onReady(function () {

    let filters = {
        'orderStatuses': ['ACTIVE']
    }

    orders.listCurrentMemberOrders(filters, null, null)
        .then((ordersList) => {
            if (ordersList.length > 0) {

                const planName = ordersList[0].planName;

                if (planName == plan1) {
                    wixLocationFrontend.to(redirect1);
                } else if (planName == plan2) {
                    wixLocationFrontend.to(redirect2);
                } else if (planName == plan3) {
                    wixLocationFrontend.to(redirect3);
                }

            } else {
                // IF NO ORDERS FOUND
                wixLocationFrontend.to("/plans-pricing");
            }
        })
        .catch((error) => {
            // HANDLE ERRORS
            console.error(error);
        })

});

Thank you Pratham for sharing the code.
I have tried to use this code:
import { orders } from ‘wix-pricing-plans-frontend’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
// Function to handle button click
function handleDeepdiveButtonClick() {
let filters = {
‘orderStatuses’: [‘ACTIVE’]
};

    orders.listCurrentMemberOrders(filters, null, null)
        .then((ordersList) => {
            if (ordersList.length > 0) {
                const planName = ordersList[0].planName;
                if (planName === "Ignite") {
                    wixLocation.to("/tnr-subs");
                } else {
                    wixLocation.to("/tnrsubsplans");
                }
            } else {
                console.log("No active orders found");
                wixLocation.to("/tnrsubsplans");
            }
        })
        .catch((error) => {
            console.error("Error fetching orders:", error);
            wixLocation.to("/tnrsubsplans");
        });
}

// Add click event listener to the button
$w("#deepdiveBtn").onClick(handleDeepdiveButtonClick);

});

However, when the button deepdiveBtn is pressed, it automatically takes the user to the No active orders found - /tnrsubsplans.

I am looking at achieving the following:

  1. When the user reaches a specific page, it has this button names deepdiveBtn
  2. When a returning user presses this, the system should check if there is an active subscription of the user in wixData
  3. If yes, then the control should move to /tnr-subs
  4. If no, the control should move to /tnrsubsplans

How can I achieve this?

When working with the listCurrentMemberOrders function, it’s essential to use the unique identifier (_id ) to check for specific orders, not the plan name. The _id field provides a reliable way to identify individual orders within your dataset.

Thank youCMSblocks.com.
The issue got resolved. I have used another approach. I used this code:

import { session } from ‘wix-storage’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
$w(“#Button1”).onClick(function () {
// Retrieve subscription details from session storage
let subscriptionDetailsStr = session.getItem(“subscriptionDetails”);

    if (subscriptionDetailsStr) {
        try {
            let subscriptionDetails = JSON.parse(subscriptionDetailsStr);
            console.log("Parsed subscription details:", subscriptionDetails);

            if (subscriptionDetails && subscriptionDetails.status && subscriptionDetails.endDate) {
                let status = subscriptionDetails.status;
                let endDate = new Date(subscriptionDetails.endDate);
                let today = new Date();

                // Check conditions for redirection
                if (status === 'ACTIVE' && endDate >= today) {
                    wixLocation.to("/Landingpageonsubs");
                } else {
                    wixLocation.to("/subsplanpage");
                }
            } else {
                console.error("Subscription details are incomplete or missing necessary fields.");
                wixLocation.to("/subsplanpage");
            }
        } catch (error) {
            console.error("Error parsing subscription details:", error);
            wixLocation.to("/subsplanpage");
        }
    } else {
        console.error("Subscription details not found in session storage.");
        wixLocation.to("/subsplanpage");
    }
});

});

Thank you for your inputs!