I was able to create an instance of a course (location/date) and when a user fills the credform registration it assigns it thew a relationship field automatically to the current location in the loop. This was possible using a custom shortcode and a generic field:
add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
if ($form_data['id']==1347) {
toolset_connect_posts('localizacao-inscricao',$_POST['@localizacao-inscricao.parent'], $post_id);
}
}
Regarding point 2 of the original question, i need to set a condition on a button to check:
If field "Registration Limit" of the current location/date cpt in the loop (this is a normal field of the cpt) <= current related posts then {
etc.. etc..
}
And this is what i'm having issues with. I ran the conditional wizard and i get this:
but it's missing the second part which is how to get the number of registrations that were already created in that location. This is for course location, so i create "Webdesign Paris" and set the field registration limit to 10. If i already have 10 registrations assigned to that location (1-M relation) and the limit is 10, i want to display a SOLD badge.
I checked the Post relationships API here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/ and i see "toolset_get_related_post_types" with the Description "Get post types related to the provided one.", but i wish to get the count... not the list.
How can i achieve this? If needed access please place answer as private.
What it does is that it will return the list of related posts. Now we don't have a function that returns the count, however you can use the count() php function on this list to get a number value of how many posts are in the relationship. hidden link
add_shortcode('registrations-count', 'registrations_count_func');
function registrations_count_func($atts) {
global $post;
$child_posts = toolset_get_related_posts($post->ID,'localizacao-inscricao','parent',999,0,array(),'post_id','child');
return count($child_posts);
}
And went to Toolset settings and registered registrations-count as shortcode to use in Views.
I posted the shortcode into the view to see if i would get a number and all i see is [registrations-count] shortcode in pure text... so it's not rendering the shortcode. I also tried adding {!{registrations-count}!} and the problem remains the same...
Nevermind... the code was in functions.php of the child theme that wasn't activated. It's rendering and apparently the number is correct, if I need any more help I will post.
Well it seems that point 1 is no longer working, although the CPT is created when cred form is submitted there is no assigment of the relationship. An error is returned:
Notice: Undefined index: @localizacao-inscricao.parent in function.php
Fatal error: Uncaught InvalidArgumentException: The parent must be a post ID or a WP_Post instance.
The form is located in the course page just below each location (which is a looped view) and the ID is 1333. The generic hidden field has name="@localizacao-inscricao.parent" and i can see it displays the number 1334 which is the ID of the instance (location/date related to the course) for which we are registering to.
I would suggest checking the payload of the page by inspecting the POST request when the form is being submitted.
Using the Chrome inspection tool, Navigate to Network tab and then click on preserve log. From there submit your form as you would then check the POST request for the name being passed.
If you're unsure of how to do this then you can provide me with access tot he form so that I can submit the form as a test to see what the name is that is showing up for this field.
we could post a screenshot of the post request but we dont wish to disclosure the domain. i could provide the URL but the site is in maintenance mode which means you must login to have access.
We are more confortable providing access to admin and you see it for yourself.
Could you please set next post as private?
Thanks, can you let me know where the code was added, I would like to do some debugging to see if I can understand what is happening here.
It may be a case where the parent value isn't being accessed in the code. What you can do to check this is to dump the variable onto the page itself to see what exactly is happening.
However please let me know where I can access the code.
Don't know why Avada doesnt show the Theme Editor.
Anyway, i installed File Manager where you can access the Avada child-theme functions.php where the code was inserted.
Out of curiosity, how would i dump the variable onto the page itself?
As you can see the index being used for the parent is "@localizacao-inscricao_parent" so in your code you should change @localizacao-inscricao.parent to @localizacao-inscricao_parent
This should resolve the issue of the unidentified index. I will leave the var_dump() function active in your code so that you can have a look at what I did to debug.
Thank you for the feedback.
Yes that worked out fine. I'll leave this thread open because I might need some help displaying the list of courses and their locations, if that's the case I will post here.