"getPricingPlan" how to show most recent purchased plan or status active plan?

im trying to show the most recently purchased plan or the active status on my members page but it seems like it only show the first plan that was purchased can anyone help me fix this?

this is my code to show the current user plan and it only show the first plan purchased i wan to show the most recently purchased plan.

import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;
import wixPaidPlans from ‘wix-paid-plans’ ;
import wixWindow from ‘wix-window’ ;

$w.onReady( function () {

let user = wixUsers.currentUser;

let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

wixUsers.currentUser.getPricingPlans()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[ 0 ];
let planName = firstPlan.name;
let qprice = firstPlan.price;
let benefits = firstPlan.benefits;
let sdate = firstPlan.startDate;
let eDate = firstPlan.expiryDate;

$w( ‘#input1’ ).value = firstPlan.name;
$w( ‘#input3’ ).value = firstPlan.startDate.toDateString();
$w( ‘#input4’ ).value = firstPlan.expiryDate.toDateString();

});

wixPaidPlans.getCurrentMemberOrders()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[ 0 ];

$w( ‘#input2’ ).value = firstPlan.status;

});

});

Hey,

This code is specifically calling for the first plan that a user has purchased because the plans themselves are an array and pricingPlans[0] would be the first item in that array.

To call for the most recent item added to an array you could use pricingPlans[pricingPlans.length-1].

So your code would look like this

import wixUsers from'wix-users';
import wixLocation from'wix-location';
import wixPaidPlans from'wix-paid-plans'; 
import wixWindow from'wix-window'; 

$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;         // "r5cme-6fem-485j-djre4844c49"
let isLoggedIn = user.loggedIn; // true


wixUsers.currentUser.getPricingPlans() 
.then( (pricingPlans) => {
let currentPlan = pricingPlans[pricingPlans.length-1];          
let planName = currentPlan.name;  
let qprice = currentPlan.price;    
let benefits = currentPlan.benefits;      
let sdate = currentPlan.startDate;        
let eDate = currentPlan.expiryDate;

$w('#input1').value = currentPlan.name;
$w('#input3').value = currentPlan.startDate.toDateString();
$w('#input4').value = currentPlan.expiryDate.toDateString();

});
wixPaidPlans.getCurrentMemberOrders()
 .then( (pricingPlans) => {
 let currentPlan = pricingPlans[pricingPlans.length-1];          
 $w('#input2').value = currentPlan.status;

  });

});

Hope this helps!

Dara | Corvid Team

hi dara,

tnx for the help but im getting this error

can you pls help me on this.

Hey!

Could you post a link to your site, so I could have a closer look?

Thanks!

Dara | Corvid Team

Hello Dara,

I tried your code on my website.
The code is OK but it doesn’t show the most recent purchased plan every time.

(I did few tests with different accounts and different plans ordered - sometimes it shows the older purchased plan…)

Can you help ?

Thx !