Mail counter in email is duplicated when two forms submitted at same time
-
I have a mail counter that gets incremented when an email is successfully sent. This mail count is visible in the email body. The problem arises when two forms are submitted at almost the same time. When this happens the email is sent to different users containing the same mail count, example 10 and 10, whereas it should be 10 and 11.
Initial code from here https://sevenspark.com/tutorials/how-to-create-a-counter-for-contact-form-7
#this code block is inserted in the email through shortcode
function RC_counter_func() {
$val2 = date( “Y” ) . ‘-‘ . zeroise( get_option( ‘RC_COUNTER’, 0 ) + 1, 5 ); //Increment the current count
return $val2;
}
add_shortcode( ‘RC_COUNTER’, ‘RC_counter_func’ );// Action performed when the mail is actually sent by CF7
function cf7dtx_increment_mail_counter( $WPCF7_ContactForm ) {#retrieve the details of the form/post
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
$form_id = $WPCF7_ContactForm->id();if ( $submission && $form_id === 988 || $form_id === 1584 ) {
$val2 = zeroise( get_option( ‘RC_COUNTER’, 0 ) + 1, 5 ); //Increment the current count
update_option( ‘RC_COUNTER’, $val2 ); //Update the settings with the new count
}
}
add_action( ‘wpcf7_mail_sent’, ‘cf7dtx_increment_mail_counter’ );Any ideas on how to prevent duplication would be much appreciated! When submitting forms one after the other there is no issue, and the number is being incremented correctly in the DB, but not in the email.
- The topic ‘Mail counter in email is duplicated when two forms submitted at same time’ is closed to new replies.