WordPress | ACF PRO & Contact form 7 Create an event subscription form
A customer ask me to create, under his wordpress website, an Events Subscribe Form, at the moment, the events, are a custom post type with some acf fields associated.
Basically he wants to have the ability to add a maximum number of users that can register to the event.
When a user subscribe to the event, if there is disponibility of places, he ‘ll recive a mail with the payment datas, otherwise, he’ll recive a mail that inform him that there isn’t more disponibility of places.
First of all create 2 custom fields associated with the custom post type “Event”:
1° field : Label:disponibility Name:disponibility Type:number
2° field: Label:subscriber number Name:subscribe_number Type:number Default: 0
Second create a Form in “Contact form 7” open the “single-events.php” from you theme, and where you want the subscribe form appears add:
<?php echo do_shortcode( '[contact-form-7 id="144" title="Subscribe Events Module"]' ); ?>
Change “id=” and “title=” with the one from your form.
Next, open the function.php from your theme and add:
add_action("wpcf7_before_send_mail", "wpcf7_do_something");
Basically this action performed when the mail is actually sent by CF7, now we have to create the “wp7_do_domething” function:
function wpcf7_do_something($WPCF7_ContactForm) { //Get current form $wpcf7 = WPCF7_ContactForm::get_current(); // get current SUBMISSION instance $submission = WPCF7_Submission::get_instance(); $val_posti = $your_postid; if ($submission) { // get submission data $data = $submission->get_posted_data(); // nothing's here... do nothing... if (empty($data)) return; // extract posted data for example to get name and change it $name = isset($data['your-name']) ? $data['your-name'] : ""; // do some replacements in the cf7 email body $mail = $wpcf7->prop('mail'); // with $data['my-filed-name'] we extract the post/page id $posti_dis = (int) get_field('disponibilita',$data['my-filed-name']); //number of places ( from custom field associated with the custom post type ) $n_iscritti = (int) get_field('numero_iscritti',$data['my-filed-name']); //number of subscribers ( from custom field associated with the custom post type ) $post_riamanenti = $posti_dis - $n_iscritti; // Number of free places if(($posti_dis - $n_iscritti)>0){ // if (number of subscribers < number of places ) $mail[ 'subject' ] .= ' - Posti Disponibili'; $mail2 = $wpcf7->prop('mail_2'); $mail2['body'] = ' Buongiorno.<br/>Abbiamo ricevuto la sua richiesta di iscrizione.<br/>A breve invieremo una email di conferma di iscrizione con i dettagli dell’evento..<br/><br/>Qualora voglia partecipare anche alla cena conviviale, per perfezionare l'iscrizione, la invitiamo ad <br/> inviare la copia del bonifico all'indirizzo email <br><a href="eventi@ugdcecmonzabrianza.it">eventi@ugdcecmonzabrianza.it</a><br/>Il bonifico dovrà essere eseguito sulle coordinate Cordiali saluti.<br/>UGDCEC Monza e Brianza'; // Save the email body $wpcf7->set_properties(array("mail" => $mail,"mail_2" => $mail2)); $n_iscritti++; // Increment the number of subscribers update_field('numero_iscritti', $n_iscritti, $data['my-filed-name']); // Save the value in the relative custom fields return $wpcf7; }else{ // if (number of subscribers >= number of places ) $mail[ 'subject' ] .= ' - Posti non Disponibili'; $mail2 = $wpcf7->prop('mail_2'); $mail2['body'] = 'Buongiorno.<br/>Abbiamo ricevuto la sua richiesta di iscrizione.<br/>Al momento abbiamo superato il numero di posti disponibili.<br/><br/>Le comunicheremo a breve se è possibile confermare comunque la sua richiesta. <br/><br/><br/>Cordiali saluti <br/> UGDCEC Monza e Brianza <br><a href="eventi@ugdcecmonzabrianza.it">eventi@ugdcecmonzabrianza.it</a>'; // Save the email body $wpcf7->set_properties(array("mail" => $mail,"mail_2" => $mail2)); return $wpcf7; } // Save the email body $wpcf7->set_properties(array( "mail" => $mail )); return $wpcf7; } }
For test it, navigate to post type, create new event, insert the number of “disponibility” in disponibility field ( the “subscriber” field must be 0 ).
Now try to overcome the disponibility limit, and you see that the email changes.
Tha’ all
Good luck !!!