Help Needed: Redirecting Customers to Specific Pages Based on Subscription Plan in Wix Code

Hi everyone,

I’m currently facing a challenge with my Wix website and could use some help. I’m trying to set up redirections so that users are taken to specific pages based on their subscription plans when they log in. Here’s a breakdown of what I’m aiming to achieve:

Vervente Plan: Redirect users to the Vervente page.

Maestino Plan: Redirect users to the Maestino page.

Annual Plan: Redirect users to the Maestrino page.

I’ve tried a few different approaches using Wix Velo (formerly Corvid), but I’m having trouble getting the code to work as expected. Here’s a snippet of what I’ve been working with:

javascript:

import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
    wixUsers.currentUser.getRoles()
        .then((roles) => {
            let userRole = roles[0].name; // Assuming users have only one role
            switch (userRole) {
                case 'Vervente':
                    wixLocation.to('/vervente-page');
                    break;
                case 'Maestrino':
                    wixLocation.to('/maestrino-page');
                    break;
                case 'Annual':
                    wixLocation.to('/maestrino-page');
                    break;
                default:
                    console.log('Role not found');
                    break;
            }
        })
        .catch((error) => {
            console.error('Error fetching roles:', error);
        });
});

Issues I’m Facing:

The redirection doesn’t always work as expected. Sometimes, users aren’t redirected to the correct page, and it’s always has an error when testing like the API?

There seems to be a delay or failure in fetching the user roles, causing the redirection to fail.
I’m unsure if I’m correctly handling users with multiple roles.

Has anyone else encountered this issue or can anyone point out what might be going wrong? Any advice or code examples would be greatly appreciated. Thank you!

Can you share the error you’re getting?

I’m encountering an issue where the site doesn’t redirect to the intended page. When I tested on the live site, using an account with the Maestrino plan, it neither redirects to the Maestrino page nor to the specified redirect page.

It might be related to the use of wix-users which is a deprecated API. I’d recommend updating the code to use wix-members-frontend and see if that makes a difference - https://dev.wix.com/docs/velo/api-reference/wix-members-frontend/current-member/get-roles

1 Like

May also want to check that this line has the correct values.

Can also adapt this to handle multiple roles for example:

switch (true) {
    case roles.some((role) => role.name === 'Vervente'):
        wixLocation.to('/vervente-page');
        break;
// etc
}
1 Like