• Resolved ewhitenack

    (@ewhitenack)


    Hello,

    I’m currently setting up a form for our agency to process prepayments for afterschool care. Forminator has been able to handle all the specific needs such as displaying the correct amount of date pickers based on the number of days of service requested and calculating the payment based on number of hours of service requested for each specific date. The one potential issue we are running into is that there is a chance we will reach capacity for some dates so we are looking to see if it’s possible for a date to be disabled once a certain amount of entries are submitted with that date selected.

    Please let me know if this is possible, thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @ewhitenack,

    Trust you are doing good and thank you for reaching out to us.

    An out-of-the-box solution is not available for this. I have pinged our developers to check if a workaround could be provided and we’ll update you here once we have feedback on this as soon as possible.

    Kind Regards,
    Nebu John

    Thread Starter ewhitenack

    (@ewhitenack)

    Okay thank you for checking on this for us.

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @ewhitenack,

    Please try this snippet as a workaround and see whether that helps:

    <?php
    
    add_action( 'forminator_form_after_save_entry', 'wpmudev_disable_dates_calendar', 10, 2 );
    add_action( 'forminator_form_after_handle_submit', 'wpmudev_disable_dates_calendar', 10, 2 );
    function wpmudev_disable_dates_calendar( $module_id, $response ) {
    	if ( $module_id != 2910 ) {
    		return;
    	}
    
    	if ( $response && is_array( $response ) ) {
    		if ( $response['success'] ) {
    			$form_meta 	= get_post_meta( $module_id, 'forminator_form_meta', true );
    			if ( $form_meta ) {
    				$update_meta = false;
    				if ( isset( $form_meta['fields'] ) ) {
    					foreach ( $form_meta['fields'] as $form_key => $form_val ) {
    						if ( $form_val['id'] == 'date-1' ) {
    							if ( isset( $_POST ) ) {
    								$date = isset( $_POST['date-1'] ) ?  sanitize_text_field( $_POST['date-1'] ) : '';
    								$date_submit = wpmudev_get_date_entries( $module_id, $date );
    								if ( $date_submit ) {
    									$form_meta['fields'][$form_key]['disabled-dates'][] = $date;
    									$update_meta = true;
    								}
    							}
    						}
    					}
    				}
    
    				if ( $update_meta ) {
    					update_post_meta( $module_id, 'forminator_form_meta', $form_meta );
    				}
    			}
    		}
    	}
    }
    
    function wpmudev_get_date_entries( $form_id, $date_val ) {
    	global $wpdb;
        $table_name       = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META );
        $entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY );
        $sql              = "SELECT m.entry_id FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.entry_id = m.entry_id) WHERE e.form_id = %d AND m.meta_key = %s AND m.meta_value = %s order by m.meta_id";
        $entry_ids        = $wpdb->get_results( $wpdb->prepare( $sql, $form_id, 'date-1', $date_val ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    	if ( ! empty( $entry_ids ) && count( $entry_ids ) >= 4 ) {
            return true;
        }
    
        return false;
    }

    You’ll need to update the line in the above code with your form ID ie:

    if ( $module_id != 2910 ) {

    Suppose the form ID is 123 the above line will change as follows:

    if ( $module_id != 123 ) {

    At the moment, the snippet will only allow the date to be entered 4 times based on the following line:

    if ( ! empty( $entry_ids ) && count( $entry_ids ) >= 4 ) {

    If you want to change that to 2 times etc then you can update the value of 4 to 2 ie as follows:

    if ( ! empty( $entry_ids ) && count( $entry_ids ) >= 2 ) {

    I gave a quick test and confirm the snippet works fine. The snippet can be added as a mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Please do let us know how that goes.

    Best Regards,

    Nithin

    Thread Starter ewhitenack

    (@ewhitenack)

    Hi Nithin,

    Thank you so much for this solution. I just implemented it to our wp-directory and tested out the form and it works perfectly. You have saved us so many hours and costs by allowing this form to disable dates after reaching the limit. You really went above and beyond by providing this solution so again, thank you.

    Evan

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Submission limits for specific dates based on previous entries’ is closed to new replies.