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;
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;
});
});