Getting member planId

Hi Everyone,

I have been out from coding long time and got out of track.
I have the code which checks if user has any of active plans and delivers me a function

$w.onReady(function () {

    user.getPricingPlans().then((plans) => {
 

 if (plans.length > 0) {
            setTimeout(
              $w("#repeater1").show(),
              $w("#group39").show()

              , 500)
              console.log("User is member ")
        } else {
      $w("#repeater1").hide();
      $w("#group40").show();
      $w("#text136").show();
      console.log("User not-subscribed")
        }
    });
})

What i am looking now is to basically have all the same, but instead get specific planId and check if the user has that plan and is it active and then keep the same IF function.
I have been playing around but without luck.

I understand that this is basics but like i said i have been totally out and my knowledge has never been so good tho :grinning:

Thank you!

@Ahmad

Hey there :raised_hand_with_fingers_splayed:

Glad you’re back, and welcome again.

Basically, after getting all the plans of a given user, loop through them and check against their IDs, if one matches then check the status of that plan, the thing is, you cannot get the ID of the plan when getting the user plans - which is a shame. however, you can get the name of the plan, I know, I know, it’s not the most accurate way, but it is what it is.

user.getPricingPlans().then(plans => {    
    const time = new Date().getTime();
    
    // Get only the active plans;
    plans.filter(items => items.expiryDate.getTime() < time);
    
    // Get the plan that matches the specified name
    plans.filter(items => items.name === 'Premium Plan');
    
    /* Now check if there are any items in the plans array, if there are 
    any items left, then the plan is found and is active */
    
    if (plans.length > 0) {
        // The user has an active plan;
    } else {
        // User doesn't have an active plan
    }
})

Hope this helps~!
Ahmad

Hi Ahmad,

Thank, nice to hear from you again - as always you are the man! It works fine.
Except that u made small mistake:

user.getPricingPlans(plans=>{

Should be

 user.getPricingPlans().then((plans)

Thank you again, i might need some help again! :sunglasses::100:

Correct :rofl: Just a typo

@ahmadnasriya Hi! This code was working properly but for some reason it is not working anymore.

Here is what the current code looks like:


$w.onReady(()=> {
    let user = wixUsers.currentUser;

    user.getPricingPlans().then(plans => {    
    const time = new Date().getTime();
    
    // Get only the active plans;
    plans.filter(items => items.expiryDate.getTime() < time);
    
    // Get the plan that matches the specified name
    plans.filter(items => items.name === 'planname');
    
    /* Now check if there are any items in the plans array, if there are 
    any items left, then the plan is found and is active */
    
    if (plans.length > 0) {
        // The user has an active plan;
         $w("#button1").show();
    } else {
        // User doesn't have an active plan
         $w("#errrortxt").show();
    }
})
})

Here is what console error says:

Do you have any idea what is wrong here? Like i said, it has been working for year or so.