Quantcast
Channel: WordPress.org Forums » [Forminator Forms – Contact Form, Payment Form & Custom Form Builder] Support
Viewing all articles
Browse latest Browse all 1447

Automatically Block Date in datapicker calendar field after submission

$
0
0

Replies: 2

Hello,

I found some code from a previous post about blocking dates that were selected for an event. I copied the code into a php file in the mu-plugins directory of my site. I changed the code form_id to my form’s id and did not have to change the date-1 field. However, after testing the code does not seem to work.

My requirement is after someone selects a date to donate flowers and submits the transaction, I want to automatically disable that specific date in the calendar so no one chooses the same date.

Can you advise me how to fix the code or suggest alternate code?

This is the code I implemented in the mu-plugins directory as block_dates.php.

<?php 

add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
if( $form_id != 6688 ) {
return $submit_errors;
}
$last_entry = '';
// Change this to the message that you want to show.
$message = 'Booking has been done for the day. Please book for another day.';
foreach( $field_data_array as $key => $value ){
if( $value['name'] == 'date-1' ){
if( $value['value'] != '' ){
$last_entry = wpmudev_get_last_entry_by_date( $form_id, $value['value'] );
}
}
}

if ( ! empty( $last_entry ) ) {
$submit_errors[]['submit'] = $message;
}

return $submit_errors;
},15,3);


add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error', 10, 2 );
function wpmudev_invalid_form_error( $invalid_form_message, $form_id ){
if( $form_id != 6688 ) {
return $invalid_form_message;
}

$last_entry = '';
if( isset( $_POST ) ){
$date = isset( $_POST['date-1'] ) ? sanitize_text_field( $_POST['date-1'] ) : '';
if( $date ){
$last_entry = wpmudev_get_last_entry_by_date( $form_id, $date );
}
}

if ( ! empty( $last_entry ) ) {
$invalid_form_message = __( 'Booking has been done for the day. Please book for another day.', 'forminator' );
}

return $invalid_form_message;
}


function wpmudev_get_last_entry_by_date( $form_id, $date ){
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_id = $wpdb->get_var( $wpdb->prepare( $sql, $form_id, 'date-1', $date ) );
if ( $entry_id ) {
return $entry_id;
}
return false;
}
add_action( 'forminator_form_after_save_entry', 'wpmudev_disable_booking_dates', 10, 2 );
add_action( 'forminator_form_after_handle_submit', 'wpmudev_disable_booking_dates', 10, 2 );
function wpmudev_disable_booking_dates( $module_id, $response ){
if( $module_id != 6688 ){
return;
}

$form_meta = get_post_meta($module_id, 'forminator_form_meta', true);
if( $form_meta ){
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'] ) : '';
if( $date ){
$form_meta['fields'][$form_key]['disabled-dates'][] = $date;
}
}
}
}
}
update_post_meta($module_id, 'forminator_form_meta', $form_meta);
}

}

Viewing all articles
Browse latest Browse all 1447

Trending Articles