Skip Navigation

[Resolved] [ATTN: Christian Cox] – continuation of "one-to-many user relationships"

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#595777

Christian,

I am finally moving into phase 2 of this project, and our previous conversation has been closed:

https://toolset.com/forums/topic/is-it-possible-to-create-a-one-to-many-user-relationship/

I would like to run some items by you, and get this party host registration form built as soon as possible.

I have more information as to how this site is being built, so I'd like to refresh the conversation. What I need to build is:

A user registration form that a consultant can give to any person who is interested in becoming a party host. That person fills the registration form out, and upon submitting, that new party host is associated to the consultant in the database. At this point, I can use this connection to show or hide content, create custom links, etc.

The first step is to have the new user meta entries in the database. I am thinking we need 2 new entries, 'party_host_id' and 'consultant_id'.

A quick note ... I am already using a couple of plugins that are generating "CTID" and "PHID". CTID is a url query string for AffiliateWP for consultants to make commission off of purchases in the store, and PHID is created by another plugin which also uses it as a query string to give party hosts points when a custom buys something in a store. These 2 items are using a user's username as the parameter.

"www.domain.com/?ctid=username&phid=username".

These are really the only 2 url query strings I need, as the one creates the cookies needed for consultants to get their commission and the other sets the cookies for party hosts to get their points. The reason I need to connect consultants and party hosts during the party host registration is so I can generate the party host's special referral url to give to customers, because ultimately, it is the party host's effort that gets the commission for the consultant. That's why I am combining bother referral urls together which includes the consultant id and the party host id.

That being said, do you suggest I use completely unique names for the user meta data that are different from the CTID and PHID, and then associate that unique meta to CTID and PHID afterward?

So, let me know if I am on the right track here:

Step 1: create user fields, 1 for consultants and 1 for party hosts.
-- consultant_id and party_host_id

Step 2: create a user form in CRED.

Step 3: add user fields to user form as hidden fields.

Step 4: auto-populate the consultant_id hidden field with the current consultant's username.
-- Perhaps I'll add a custom button that has 1 field for the recipient's email address, and that button emails the recipient.
-- I will be using 1 user form, and when a consultant wants to send it to a new party host, they'll go to the page with the special email button and the form, at which time their username will be auto-populated into the button, so when they send the link to the potential party host, it adds a query string to the url that will auto-populate the hidden consultant_id field with the referring consultant's username.

QUESTION: at this point, when the new user fills the form out and submits is, the consultant_id should be added to the user meta for that new user. At the same time, since we are letting new users pick their usernames, can we auto-populate the party_host_id hidden field with the new user's username?

Step 5: when the new user submits the form, the consultant_id should be added to the user meta for that new user, and the party_host_id has to be added to the referring consultant's user meta.

QUESTION: Is it enough to only add 'consultant_id' user meta to the party host users, and 'party_host_id' user meta to the consultant users? To clarify, username1 is a consultant, and username2 is a new party host. I want the new party host to be able to go to their dashboard to see their referral url. That referral url has to include their consultant's referral code: "www.domain.com/?ctid=username&phid=username". So, on that party host's dashboard, I would use some php to go into the usermeta, find the consultant_id and grab the consultant's username to spit into the "CTID" query string. I also want a consultant to be able to go to their dashboard, and see a list of all of the party hosts under them. So, 1 consultant can have many party hosts associated to them, but a party host can only have 1 consultant associated to them.

I hope I am making sense. I am trying to get us back into the swing of this, so I wanted to give you possibly more info than you need.

At this point, do you have a pretty good idea of where I am going?

I looked at our previous conversations, and I'm having trouble jumping in.

I'd love picking up where we left off.

Thanks so much, and I look forward to hearing from you!

#595940

That being said, do you suggest I use completely unique names for the user meta data that are different from the CTID and PHID, and then associate that unique meta to CTID and PHID afterward?
Depends on whether or not any of these values can change over time on either system. If so, then an abstraction is probably a good idea. If not, then it's probably not necessary.

QUESTION: at this point, when the new user fills the form out and submits is, the consultant_id should be added to the user meta for that new user. At the same time, since we are letting new users pick their usernames, can we auto-populate the party_host_id hidden field with the new user's username?
Sure, you can use the cred_save_data hook to define any usermeta field upon form submission. Something like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        $user_info = get_userdata(1);
        $username = $user_info->user_login;
        add_user_meta($user_id, 'party_host_id', $username, true);
    }
}

QUESTION: Is it enough to only add 'consultant_id' user meta to the party host users, and 'party_host_id' user meta to the consultant users?
I think so, you can use this info to build your link URLs, show the consultant lists of their hosts / URLs, and show the hosts their relevant URL.

#596107

Christian,

Based on this new email string, it seems we've simplified this quite a bit!

To take this all the way home (hopefully), this is exactly what I am going to build ... please let me know if you see any issue with it:

I decided to use user id instead of username for these fields, because I can query more with WordPress using a user's user id. Do you agree?

-2 user meta fields: consultant_id and party_host_id
-Button on consultant's dashboard to send a invite link to potential party hosts
----Button is pulling in user id of consultant and adding it to the invite url to send to the hiddem field in the registration form for consultant_id.-
-Registration form
----Hidden field for consultant_id (pulling in from invite url)
--------Does this automatically get added to the user meta of the registrant because it is a CRED user form? Unlike the party_host_id which has to be sent back to the referring consultant via cred_save_data?
--------Should I also add consultant_id to the referring consultant meta via cred_save_data? I feel like it would be easier to make a connection if both consultants and party hosts had both consultant_id and party_host_id ... that way a simple conditional would be an option.
--------If I don't add consultant_id to the consultant user meta, I can still use a conditional by comparing consultant_id in a party host's user meta to the consultant's actual user id.
--------Out of the 2 options above, what would be your preference?
-Hidden field for party_host_id
----Since I changed these fields to pull in user id, can this hidden field still be populated with cred_save_data? Once the user submits the registration form, they are assigned a user id, and I can submit this user id to party_host_id meta data via cred_save_data?
-Users are now connected, and I can write custom queries with conditionals that filter based on the user relationship.

The 2 potential actions I need when the form is submitted ... please confirm:


<strong>Action adding meta data to consultant</strong>

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data) <----- Is this user id the id that is created for the new user when the form is submitted? I see below that you are using this as the user id of the consultant, so the party host meta can be sent. This seems off.
{
// if a specific form
if ($form_data['id']==12345)
{
$user_info = get_userdata(1); <----- Where is this 1 coming from? Is this just a default you put in there, or is it always going to be 1 when a form is submitted? I though the value in the parenthesis was $userid?
$username = $user_info->user_login;  <----- changed my mind about username ... should be user id of party host
add_user_meta($user_id, 'party_host_id', $username, true);
}
}

<strong>ALTERNATIVE:</strong>

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{ // if a specific form
if ($form_data['id']==12345)
{
// set consultant_id, pulling from the consultant_id hidden field that pulls the value from a url parameter, correct?
$consultant_id = $_POST['consultant_id'];
// meta_value set by using $user_id which is pulling the new registrant's id, correct?
add_user_meta($consultant_id, 'party_host_id', $user_id, true);
// potentially add the consultant_id to the consultant's user meta, as well ... thoughts?
add_user_meta($consultant_id, 'consultant_id', $consultant_id, true);
}
}


<strong>Action adding meta data to party host/registrant</strong>

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{ // if a specific form
if ($form_data['id']==12345)
{
// set consultant_id, pulling from the consultant_id hidden field that pulls the value from a url parameter, correct?
$consultant_id = $_POST['consultant_id'];
// Add the party_host_id to the new party host ... what do you think? meta_value set by using $user_id which is pulling the new registrant's id, correct?
add_user_meta($user_id, 'party_host_id', $user_id, true);
// Add the consultant_id to the party host's user meta
add_user_meta($user_id, 'consultant_id', $consultant_id, true);
}
}

Does this all make sense, or did I botch something up? Is there any overkill I can cut out?

Thanks!!

#596111

Christian,

I just realized while building this form out that the 'party_host_id' doesn't need to pull a value from anywhere, as we are submitting this value directly to the user meta via the cred_save_form. I still need a hidden field with a blank value here, so the user meta is created, or was a user meta field already created in the database before I created the actual user fields, and I don't need a hidden field in the actual form?

I tested my consultant_id field, and it is pulling the url param just fine.

[cred_generic_field field='consultant_id' type='hidden' class='' urlparam='consultant_id']
{
"required":0,
"validate_format":0,
"default":""
}
[/cred_generic_field]

[cred_generic_field field='party_host_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":""
}
[/cred_generic_field]

Thanks!

#596112

I just had another thought .... do I even need party_host_id?

This is a one to many relationship I am trying to create, where 1 consultant can have many party hosts, but a party host can only have 1 consultant.

I am unable to add multiple values to the party_host_id meta in the consultant's user meta ....

Wouldn't that mean that all I need to do is add 'consultant_id' to the party host's user meta, which associates a parent consultant to a party host, and in doing so associates that party host to the consultant?

Then, I am always querying user meta to search for 'consultant_id':

So, if I want to query users, and list out all of a consultant's party hosts, I would get all users, and then filter using a conditional where I check the currents user's id against the 'consultant_id' of all users, effectively generating a list of all party hosts of that consultant.

Additionally, if I want to create the custom referral url (hidden link):
where PHID is the current user's user id, and CTID is the current user's user meta of 'consultant_id.

Is this making sense, or do you feel I will eventually need party_host_id populated into user meta for something? If so, how would I make that user meta of party_host_id an array of multiple party hosts within 1 consultant's user meta?

Just thinking this though. Thank you again!!

#596330

Hi, this thread is becoming a bit difficult to follow, do you agree? I must insist that you limit the scope of each ticket in the future. It's not practical for either of us to consider 30 questions in a single ticket.

I decided to use user id instead of username for these fields, because I can query more with WordPress using a user's user id. Do you agree?
I am not sure about that. I encourage you to check the WordPress documentation for more information about User queries.

Does this automatically get added to the user meta of the registrant because it is a CRED user form?
A generic field is not automatically added to user meta. If you insert the actual field in the CRED form instead, then you can hide it with CSS instead. Otherwise, you'll have to use the cred_save_data hook to read the generic field and copy it into the user meta field.

Should I also add consultant_id to the referring consultant meta via cred_save_data?
If it makes your queries easier, sure. But it seems like it will require an extra database query to get matching usermeta based on a known user ID.

Once the user submits the registration form, they are assigned a user id, and I can submit this user id to party_host_id meta data via cred_save_data?
Yes, the new User's ID is available in the cred_save_data hook as the first parameter passed into the callback function. In the docs it's usually called "$post_id" but for User forms this actually refers to User ID.

function my_save_data_action($user_id, $form_data) <----- Is this user id the id that is created for the new user when the form is submitted? I see below that you are using this as the user id of the consultant, so the party host meta can be sent. This seems off.

Yes, it's the User ID created for the new User. If I misunderstood what needs to be updated in usermeta, my apologies.

$user_info = get_userdata(1); <----- Where is this 1 coming from? Is this just a default you put in there, or is it always going to be 1 when a form is submitted? I though the value in the parenthesis was $userid?

It's just the default. You should replace it with whatever user Id you want to insert the usermeta information.

// set consultant_id, pulling from the consultant_id hidden field that pulls the value from a url parameter, correct?

That's correct.

// meta_value set by using $user_id which is pulling the new registrant's id, correct?

Yes, that's right.

// potentially add the consultant_id to the consultant's user meta, as well ... thoughts?

Do you have a code sample that shows how querying with usermeta is easier than querying with user ID?

I still need a hidden field with a blank value here, so the user meta is created, or was a user meta field already created in the database before I created the actual user fields, and I don't need a hidden field in the actual form?
If you're setting the value in cred_save_data based on a known value, there's not a need for a hidden field.

I am unable to add multiple values to the party_host_id meta in the consultant's user meta ....
Unless there's some reason I'm not aware of, you can make the party_host_id field accept multiple values. This can be configured in the field editor screen. Then usermeta updates are a bit different because you may have to specify which particular instance of the repeating value must change, or if you simply need to add an instance to the existing instances. Refer to the WordPress documentation for update_user_meta and update_post_meta for more information about this. If you have specific questions about that, please open a separate ticket.

#596342

Christian,

I'm so sorry ... I got carried away, and I'll be more careful with my tickets in the future. I realize you have gone above and beyond to help me understand how to use CRED for this task. I will build what I think it should be, and if I run into problems, I'll let you know. Otherwise, thank so much for the support.

#596391

No worries, I'll mark this ticket as pending an update from you. No need to reply right away.