I want elements to hide/show depending on a users paid plan level. If user is not logged in or only has the free access to the site then element #text104 will just be a short line of the paragraph I want users to see. I have shared the whole page code to see if there is any conflicts across the whole code.
// API Reference: https://www.wix.com/velo/reference/api-overview/introduction // “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world import wixUsers from 'wix-users'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; $w.onReady( () => { if(wixUsers.currentUser.loggedIn) { } else{ $w("#videoPlayer1").hide(); $w("#text113").show(); $w("#text104").text=$w("#text104").text.slice(0, 50); } } ); export function btnfavourite_click(event) { // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4 // Add your code for this event here: if(wixUsers.currentUser.loggedIn===true){ let $item = $w.at(event.context); let currentItem = $item("#dataset1").getCurrentItem()._id; let favorite=$item("#btnfavourite").style.backgroundColor; // console.log("Choosen data record is: " + JSON.stringify(currentItem)); if(favorite !=="red"){ let userid=wixUsers.currentUser.id; let toInsert = { "userId": userid, "classId": currentItem, }; wixData.insert("Favourite", toInsert) .then( (results) => { let item = results; //see item below console.log("Success"); $item("#btnfavourite").style.backgroundColor="red"; $item("#favMsg").text="Added to favourite!"; $item("#favMsg").expand(); } ) .catch( (err) => { let errorMsg = err; console.log(errorMsg); } ); } else { wixData.query("Favourite") .eq("classId", currentItem) .eq("userId",wixUsers.currentUser.id) .find() .then((result)=>{ let ID = result.items[0]._id; wixData.remove("Favourite", ""+ID+"") .then( (results) => { let item = results; //see item below $item("#btnfavourite").style.backgroundColor="white"; $item("#favMsg").text="Removed from favourite!"; $item("#favMsg").expand(); } ) .catch( (err) => { let errorMsg = err; } ); }) } } else{ let $item = $w.at(event.context); $item("#favMsg").text="Please login first!"; $item("#favMsg").expand(); } } export function text113_click(event) { let $item = $w.at(event.context); wixLocation.to("https://www.triplegoddessyoga.com/plans-pricing"); // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4 // Add your code for this event here: } export function repeater1_itemReady($item1, itemData1, index1) { if(wixUsers.currentUser.loggedIn) { wixUsers.currentUser.getPricingPlans() .then((pricingPlans) => { if(pricingPlans.length>0){ $item1("#videoPlayer1").show(); $item1("#text113").hide(); if(itemData1.url !==""){ $item1("#videoPlayer1").src=itemData1.url; } } else{ $item1("#videoPlayer1").hide(); $item1("#text113").show(); let paragraph=itemData1.description; var shortText=paragraph.slice(0,50); $item1("#text104").text=shortText+' ...'; } 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; }); wixData.query("Favourite") .eq("userId", wixUsers.currentUser.id) .find() .then( (results) => { if(results.items.length > 0) { let firstItem = results.items[0]; //see item below results.items.forEach(function(item,i){ let classid=item.classId; //console.log("Class=> "+classid); $w("#repeater1").forEachItem(($item, itemData, index) => { let currentText = $item("#classID").text; if(currentText===classid){ $item("#btnfavourite").style.backgroundColor="red"; } }); }); } else { // handle case where no matching items found } } ) .catch( (err) => { let errorMsg = err; $w("#favMsg").text=""+errorMsg+""; $w("#favMsg").expand(); } ); } else{ $item1("#videoPlayer1").hide(); $item1("#text113").show(); let paragraph=itemData1.description; var shortText=paragraph.slice(0,50); $item1("#text104").text=shortText+' ...'; } }
Thanks this is working, I just need to adapt this code. I want "#text104" to appear as a few lines of text and not the full lot until the user is logged in
let paragraph = itemData1.description; var shortText = paragraph.slice(0, 50); $item1("#text104").text = shortText + ' ...';
Thanks - but I want a specific plan to access certain elements. I have free plan and exclusive members.
If i replace Premium with Exclusive members will this reveal the elements for Exclusive members and hide it from free plan members?
Hi there 🖐
Use the getPricingPlans( ) method to determine the current plan of the user, then act accordingly, it's that simple.
import wixUsers from 'wix-users';
const user = wixUsers.currentUser; wixUsers.currentUser.getPricingPlans().then(plans => { const premium = 'Premium'; // The paid plan that users must have const free = 'Free Plan'; // The free plan that users must have user.plans = { premium: false, free: false }; for (const plan of plans) { if (plan.name.toLowerCase() === premium.toLowerCase()) { user.plans.premium = true; break; } if (plan.name.toLowerCase() === free.toLowerCase()) { user.plans.free = true; break; } } // Check user plans and show the suitable elements if (user.plans.premium) { // Show the elements meant for paid members. } if (user.plans.free) { // Show the content for members who have the free plan } })
Hope this helps~!
Ahmad