Display repeater data based on logged in member price plan

Hello,
I have a repeater linked to the pricing plan database. What I would like to do is only display the pricing plan based on the logged in member.
As an example, say I have 3 pricing plans - A , B , C
A member called ‘John’ who has purchased plan ‘A’ logs in. He will only see ‘pricing plan A’ displayed in the repeater.
A member called ‘Sam’ who has purchased plans ‘A and B’ logs in. He will see both ‘pricing plans A and B’ displayed in the repeater.

As a note, I don’t need to check whether a user is logged in or not as this repeater gets displayed on a page that is private and only members that are logged in can access it.

Here’s the code I have so far:

import wixPaidPlans from ‘wix-paid-plans’ ;

$w.onReady( function () {

$w( “#repeater1” ).onItemReady(($item, itemData, index) => {
wixPaidPlans.getCurrentMemberOrders()
.then ( orders => {
$w( “#repeater1” ).data;
} )
. catch ( (err) => {
console.log(err);
} );
});
});

Right now, with this code all three pricing plans ‘A , B , C’ are getting displayed. So even if John has only purchased plan ‘A’ , he’s seeing B and C listed in the repeater as well.

Would appreciate any help with this.

Thanks so much.

Hi there! This isn’t an exact answer but it should get you going in the right direction.
You could probably do this by using the Roles assigned when a member gains access to a plan. I don’t use the Paid Plans app, but on a page of my site I display content based on a user’s Role by expanding/collapsing relevant strips. It’s at least a start!

This post looks like it could be useful to you also.

Here’s my code where I check a user’s Role and expand/collapse elements based on their Role (you will have to change it to better suit your needs) :

import wixUsers from 'wix-users';

let user = wixUsers.currentUser;

$w.onReady(function () {
    $w('#stripLoading').expand();
 if (user.loggedIn) {
        user.getRoles()
            .then((roles) => {

 if (roles.some(r => r.name === "Grade 1")) { 
                    $w('#stripLoading').collapse();
                    $w('#stripG1').expand();
                } else {
                    $w('#stripLoading').collapse();
                    $w('#stripNoOrders').expand();
                }

 if (roles.some(r => r.name === "Grade 2")) {
                    $w('#stripLoading').collapse();
                    $w('#stripG2').expand();
                } else {
                    $w('#stripLoading').collapse();
                    $w('#stripNoOrders').expand();
                }
            });
    }
})

Thanks @lmeyer …will give this a try and see where it leads :slight_smile:

I’ve been looking things up to find a solution because now I want to do this on my site (show repeater items based on Role) :sweat_smile: I guess we’ll see who gets it working first!
Another option could be writing code to filter the dataset, probably by roleId. Here are some resources I found which could help us both out.
https://www.wix.com/corvid/forum/community-discussion/filtering-a-dataset-by-user-not-owner
https://www.wix.com/corvid/forum/community-discussion/filtering-repeater
https://www.wix.com/corvid/reference/wix-dataset/dataset/setfilter
https://support.wix.com/en/article/corvid-wix-pricing-plans-paidplansplans-collection-fields#role-id-roleid

Great! Let me know how it goes. I most likely won’t have the time until later on today to get back on this and try a few things. Thanks so much for all the resources you sent…will have a look.

Hey there, just curious if you were able to make it work?

Are you querying the database (in your case for member roles) and displaying it in the repeater?

Try keeping it simple. There is an event function for the Paid Plans app that gets triggered after a purchase happens.

You can add this to the backend code.

What it does is allow you to get certain pieces of data from the purchase. For example, the plan ID or Role ID or the member ID that purchased the plan, etc.

You can use this code to insert that data into a custom database collection. For example, something called: Purchases

Then you can connect the repeater directly to this collection by using a dataset. If you stored the Member ID from the Purchase, then you can filter by current user logged in ID to match that ID.

https://www.wix.com/corvid/reference/wix-paid-plans-backend/events/onplanpurchased
(event.order.memberId)

https://www.wix.com/corvid/reference/wix-users/currentuser

@arjunchahal Not yet! I got it half working, but users with multiple roles only show up one item instead of the 2 or 3 they have access to. I’ll probably end up trying a different approach.
I would try Code Queen Nayeli’s approach! It looks like a much more elegant solution for you (won’t work for me though). Good luck with everything!

Thanks so much for this. Please accept my apology for the questions that are going to follow as I’m not a coder.

As per the instructions, I have created an events.js file under backend with the following code:

import wixData from ‘wix-data’ ;

export function wixPaidPlans_onPlanPurchased(event) {

let orderData = {
“title” : “Member plan purchased” ,
“data” : event.order.memberId
};

wixData.insert( "Purchases" , orderData); 

}

I have created a database collection called ‘Purchases’. I have published my website and have conducted a few test purchases (of $0 value) through the live ‘book online’ page but the database is not getting populated with any member information.

Appreciate any help. Thank you.

@arjunchahal Your code syntax / structure looks correct.

Make sure the database you created also has those fields (and that you published your changes). In the sample code you provided you have a field called title and a field called data. These must also be fields / columns in your Purchase database. So if those fields / columns do not exist, then the information will not get saved anywhere.

Also, make sure that the database Id is in fact ‘Purchases’ and not ‘purchases’ or else the code will assume the database collection does not exist if it finds no matches.

You may want to consider changing the static word variable “Member plan purchased” to a dynamic variable such as: event.order.planId to save the Plan ID to possibly search / query / filter it later. Right now the actual words “Member plan purchased” will be saved into the database.

Thank you @code-queen

I have checked and re-checked everything numerous times before deciding to post again here but I still haven’t been able to get the dataset to populate.

My site is published. I am attaching a screenshot of my ‘Purchases’ dataset collection as well as the updated code. Also attaching a screenshot of the permissions just to ensure that is not conflicting in any way.

import wixData from ‘wix-data’ ;

export function wixPaidPlans_onPlanPurchased(event) {

let orderData = {
“planTitle” : event.order.planId,
“data” : event.order.memberId
};

wixData.insert( "Purchases" , orderData); 

}

@arjunchahal Have you tried changing ‘Purchases’ to ‘purchases’ ?

Your collection name may have a capital letter, but your collection ID may not.

@code-queen I just did…to make sure but still didn’t work.

Also, I double checked and it is a capital ‘P’ (see screenshot)

Database is populating now …thanks so much for all the help and the immense patience.

On to the next step :slight_smile:

@arjunchahal Glad I could help and happy to hear you got it working!

@code-queen well unfortunately the mission ain’t over yet :wink:

I’m getting the ‘Purchases’ database to populate with MemberID and PlanID.

I have another data collection called ‘Program Plans’ that houses all the relevant information for each program such as program number, title, description, purpose, background image for the repeater etc. I also have a reference field called MemberID from the ‘Purchases’ database to link the two data collections.

I have connected my repeater to the ‘Program Plans’ dataset to automatically populate it with all the relevant information.

When I try to filter based on owner = logged in user, on my live published site, I get a blank. And I guess that makes sense, because in this case, the content hasn’t been created by the logged in user.

Instead, when I try to add another filter…this time using MemberID (from Purchases data collection) as a reference field in the ‘Program Plans’ data collection, I am unable to add it as I get the message “none of your current datasets are valid for this filter”

From further digging on the forum, I realize I need to add some additional code for this to work. I have added this so far…

let user = wixUsers.currentUser;
let userId = user.id;

wixData.query( “ProgramPlans” )
.eq( “_id” , “user.Id” )
.find()
.then( (results) => {
if (results.items.length > 0 ) {
let items = results.items;

Am I heading in the right direction? I’m still unclear on how to actually filter the repeater content based on logged in user. Would appreciate some guidance. Thank you so so much.

@arjunchahal I like where your head is at. Yes, you are making moves in the right direction.

Now let’s take a look one step at a time …

In the purchases database: make the plan ID the reference field, and the member ID can be a regular text field.

Connect your repeater to Purchase database. As you connect your elements, you will now see the purchase plans reference fields as options to connect to.

Now you are ready to create a dataset filter on the purchase dataset that is connected to your repeater. Get the current member ID and search for matching member ID field in the dataset. (Do not search for owner. The event code will make the Admin as owner of the record.)

So … how did you do? :upside_down_face:

@code-queen Thank you once again for your help. I have made some progress this morning and feel I’m getting closer and closer :slight_smile:

In the ‘Program Plans’ data collection, I added another column called ProgramID (made it primary) and manually added the unique ID’s for all the pricing plans from the read-only Plans data collection. (is there a better, more efficient, less prone to error way to accomplish this?)

Next as per your feedback, I have made the plan ID a reference field in the ‘Purchases’ data collection …referencing the ‘Program Plans’ data collection.

I connected my repeater to the ‘Purchases’ data collection and was able to connect all the repeater elements to the ‘Program Plans’ data collection via referencing.

Now I’m a bit confused with the logic. As I mentioned earlier, the ‘Purchases’ data collection has two fields (MemberID and PlanID) that get populated automatically using the events.js code. So by making the PlanID field as a reference field (connected to the Program Plans data collection), it seems I am breaking that logic. See screenshot below.

@arjunchahal I can see why you would be confused. Instead of 2 collections, we are actually dealing with 3 collections. Ok, so that definitely changes our logic … again.

Your goal is to display the extra data that you added into your custom plans database.

What is happening now, with the existing logic, is that the plan reference field won’t match your custom database collection. The reason is because when the purchase event happens, the code is taking the plan ID from the actual Paid Plans database instead of the plan ID from the custom database collection.

(And we definitely cannot add a reference field into the read-only plans database.)

This is where you have to get creative to reach your end goal. While there are many fluid and clunky approaches you can take at this point … if this were my site, I would:

Add a custom to insert a new record into the custom plans database. This way I can tell the code which specific ID number I want for each record. Meaning I can use the same ID for both database collections.

(Don’t worry. 2 different collections can have the same ID.)

Something like: OnReady insert a new record, set _id to be ‘this’. I would repeat this for each paid plan. All you really need to insert is the _id because you can manually edit the rest of the fields without inserting them via code.

This would result in: having the same matching _id so that when the code filters the dataset, it actually does find a valid ID via the reference field.

(I hope I did not confuse you even more … )

@code-queen I think I understand what you’re saying but just before I go down this rabbit hole and make a bunch of changes, I want to be absolutely sure I’m doing the right thing :slight_smile:

So here’s the setup with some screenshots.

The read-only price plans has an ID field

I have added this same ID (manually) to my custom Program Plans data collection. This is the data collection with all the information for the repeater.

And in the the 3rd data collection called ‘Purchases’ that is linked to the events.js backend code, I added a 3rd field called ProgramID that is referencing the ID I manually entered in the ‘Program Plans’ data collection.

So now, in this data collection I have 3 fields:

  1. MemberID - gets populated automatically when the pricing plan function is called
  2. PlanID - gets populated automatically when the pricing plan function is called
  3. ProgramID - reference field connected to the ‘Program Plans’

As per your instructions in the last post, should I then remove this 3rd field (ProgramID) from the ‘Purchases’ data collection?

Also, you mentioned adding " OnReady insert a new record, set _id … " Where exactly am I adding this code? On the page I am planning to display the repeater?