Show remaining number of submissions
-
I’ve created a simple online course booking form.
Each course session can only have a limited number of participants.
I’ve set this up with the “limit submission” field and I assume all is working okay.What I’d like to do is create a text/calculation field that shows the remaining number of places for the selected session.
Is there a way of doing this?
Thanks
The page I need help with: [log in to see the link]
-
Hi @bobjob
I hope you’re doing fine today!
Out of the box there’s no option to show number of remaining submissions but I’ve consulted it with one of our developers and he came up with a solution.
Assuming that you are “Lifespan” option (in form’s “Behavior” settings) set to expire by submissions, you can use additional code that will such information to the form.
To apply the code to site:
– create an empty file with a .php extension (e.g. “forminator-form-submissions-info.php”)
– copy and paste following code into it<?php if ( ! defined( 'ABSPATH' ) ) { exit; } // No need to do anything if the request is via WP-CLI. if ( defined( 'WP_CLI' ) && WP_CLI ) { return; } add_filter( 'do_shortcode_tag', function( $output, $tag, $atts ){ error_log( '$atts: ' . print_r( $atts,true ) ); if ( 'forminator_form' !== $tag || ! isset( $atts['id'] ) || ! method_exists( 'Forminator_Form_Entry_Model', 'count_entries' ) ) { return $output; } $form = Forminator_API::get_module( (int) $atts[ 'id' ] ); if ( ! isset( $form->settings[ 'form-expire' ] ) || 'submits' !== $form->settings[ 'form-expire' ] || ! isset( $form->settings[ 'expire_submits' ] ) ) { return $output; } $allowed_submits = (int) $form->settings[ 'expire_submits' ]; $submmissions_count = (int) Forminator_Form_Entry_Model::count_entries( $atts['id'] ); $remaining_count = $allowed_submits - $submmissions_count; if ( $remaining_count <= 0 ) { return '<p>' . __( 'Sorry, you can\'t submit this form any more' ) . '</p>'; } return '<p>' . sprintf( __( 'This form has been submitted %d/%d. Remaining submissions : <strong>%d</strong>' ), $allowed_submits, $submmissions_count, $remaining_count ) . '</p>' . $output; }, 10, 3 );
– save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation; if there’s no “mu-plugins” folder directly in “wp-content”, just create empty one first.
You can also modify the message displayed if the form cannot be submitted anymore in this line
return '<p>' . __( 'Sorry, you can\'t submit this form any more' ) . '</p>';
and the message about number of submissions allowed/made/remaining in this line
return '<p>' . sprintf( __( 'This form has been submitted %d/%d. Remaining submissions : <strong>%d</strong>' ), $allowed_submits, $submmissions_count, $remaining_count ) . '</p>' . $output;
Best regards
AdamThanks Adam for the fast response!
I’ll give it a go.
Hi Adam,
Looking at the code, this seems to be counting/displaying the number of submissions of the form as a whole?
I want to be able to show the remaining submissions for a particular option in a particular “select” field. Is this possible?
Or is “$atts[‘id’]” updated depending on the current options selected in the various select fields?
Thanks
Hi @bobjob
Thanks for response!
Actually, it turns out I completely misunderstood your request initially. You was referring to the limits of “select” field options from the start and I somehow “got stuck” at the form submissions limit.
So maybe you’ll find previous code useful anyway but the one you need now instead would be this one:
<?php add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) { $GLOBALS['form_id'] = $model_id; function wpmu_forminator_select_limit_display( $html, $id, $required, $options ) { global $form_id; $field_name = str_replace( '-field', '', $id ); $select_ids = array( 'select-1-field' ); if ( in_array( $id, $select_ids ) ) { foreach ( $options as $option ) { //$option_value = isset( $option[ 'value' ] ) ? $option[ 'value' ] : false; $option_value = isset( $option[ 'label' ] ) ? $option[ 'label' ] : false; if ( ! $option_value ) { continue; } $entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_name, $option_value ); $remaining = (int) $option[ 'limit' ] - $entries; if ( 0 >= $remaining ) { continue; } $html .= "<p>" . $option['label']. " submission limit: " . $option['limit']; $html .= " Still another {$remaining} to go, so hurry up!"; } } return $html; } add_filter ( 'forminator_field_single_markup', 'wpmu_forminator_select_limit_display', 10, 4 ); return $wrappers; }, 10, 2 );
You can add it to the site the same way as you did with previous code.
In this line
$select_ids = array( 'select-1-field' );
you need to specify select field(s) that you wish it to be applied to (if there are multiple select fields in form. Note that there has to be “-field” suffix added to field ID that you see in form builder. But I think it’s pretty self-explanatory example.
And in these lines you can adjust the message:
$html .= "<p>" . $option['label']. " submission limit: " . $option['limit']; $html .= " Still another {$remaining} to go, so hurry up!";
This will add information right below the select field.
Since it’s a custom code, I’m afraid that’s pretty much all the way I can go with this but if you need to expand/modify it, I believe it should give a solid starting point ??
Best regards,
AdamHi @bobjob
I hope you are doing well and safe!
We haven’t heard from you in a while, I’ll mark this thread as resolved.
Feel free to let us know if you have any additional questions or problems.
Best Regards
Patrick FreitasHello,
Apologies for the slow response.
I finally gave your code a go tonight, but I don’t see any text about the remaining submissions etc. on my form.
What I’ve done…
1) Copy+pasted the provided code into “forminator-form-submissions-remaining.php”
2) Changed “$select_ids = array( ‘select-1-field’ );” to “$select_ids = array( ‘select-2-field’ );”
3) Uploaded the file to /wp-content/mu-plugins with file permissions set to 604Are there any other steps I should be following?
Will this apply to all of my forms? Or is there a way to limit this to a specific form?Thanks!
Hi @bobjob,
I tested the code on a test site and it works fine at our end. Can you please change the file permission to 644 and check if that helps?
The provided code will be applied to all the forms. Yes, you can limit the code to be applied on a specific form using the form ID. Please find the updated code below. Along with the select field ID, you need to change the form ID in the code.
<?php add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) { $GLOBALS['form_id'] = $model_id; function wpmu_forminator_select_limit_display( $html, $id, $required, $options ) { global $form_id; $list_form_ids = array(2488); if( in_array( $form_id, $list_form_ids ) ) { $field_name = str_replace( '-field', '', $id ); $select_ids = array( 'select-1-field' ); if ( in_array( $id, $select_ids ) ) { foreach ( $options as $option ) { //$option_value = isset( $option[ 'value' ] ) ? $option[ 'value' ] : false; $option_value = isset( $option[ 'label' ] ) ? $option[ 'label' ] : false; if ( ! $option_value ) { continue; } $entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_name, $option_value ); $remaining = (int) $option[ 'limit' ] - $entries; if ( 0 >= $remaining ) { continue; } $html .= "<p>" . $option['label']. " submission limit: " . $option['limit']; $html .= " Still another {$remaining} to go, so hurry up!"; } } } return $html; } add_filter ( 'forminator_field_single_markup', 'wpmu_forminator_select_limit_display', 10, 4 ); return $wrappers; }, 10, 2);
You can add the form ids in the following array from the code.
$list_form_ids = array(2488);
I hope that helps.
Best Regards,
Nebu JohnHi @bobjob
We haven’t heard from you in a while, I’ll mark this thread as resolved.
Feel free to let us know if you have any additional questions or problems.
Best Regards
Patrick FreitasHello,
I’m currently in need of same feature. I did everything as you described above but code seems to not work. I have created a file in mu-plugins folder copied code and by default the file has 644 permission. Also I have a follow up question, is it possible to display limit in select option label? Let me know if you need anymore info
Thanks in advance
-
This reply was modified 3 years, 6 months ago by
thortor.
Hi @thortor
Please make sure that you’ve set correct form ID in this line
$list_form_ids = array(2488);
and also correct ID of select field here:
$select_ids = array( 'select-1-field' );
If it still doesn’t work, please start a separate ticket of your own and we’ll assist you there.
Please also start another separate thread about displaying limit and we’ll check it (I think it should be doable with a bit of custom code).
Best regards,
AdamHi,
I created separate topic under this url
https://www.ads-software.com/support/topic/show-remaining-number-of-submissions-of-select-field/Cheers
-
This reply was modified 3 years, 6 months ago by
- The topic ‘Show remaining number of submissions’ is closed to new replies.