Event Organiser Pro allows you to add additional fields to your booking form. You can use this to collect additional information about the bookee.
Sometimes you’ll want to display this information in the confirmation e-mail (or indeed or other e-mails to the bookee). Event Organiser allows you to use the %form_submission%
tag in e-mail messages (see e-mailing bookees, and confirmation e-mail option) to list the additional information collected from the user when they placed their booking.
Sometimes however, you may wish to use this information inside the body of text. It would be convenient if, for instance, the tag %booking_field{12}%
were recognised by the plug-in and replaced by the information entered by the user (for the form field with ID 12). This is what the snippet below does.
First we define a helper class which will parse a given text (which may contain placeholder tags, like %booking_field{12}%
) and replace any tags with the appropriate data (using the booking ID passed to it).
class My_Booking_Fields_Email_Tag_Parser{
function __construct( $booking_id ){
$this->booking_id = $booking_id;
}
function parse( $message ){
$pattern = '/%booking_field{([^{}]*)}%/';
return preg_replace_callback( $pattern, array( __CLASS__, 'parse_tag' ), $message );
}
function parse_tag( $matches ){
$element_id = $matches[1];
return eo_get_booking_meta( $this->booking_id, 'meta_'.$element_id, true );
}
}
This parser will recognise any tag of the form %booking_field{X}%
where X
is a field ID.
Next we use this parser on the eventorganiser_email_template_tags
filter, which filters the text of the body after any default tags have been parsed. This allows us add support for additional tags (like %booking_field{X}%
) we may wish to add:
function my_custom_email_tag( $parsed_message, $booking_id, $original_message ){
//$parsed_message Email body with tags parsed
//$booking_id The booking ID
//$original_message Email body without tags parsed
$parser = new My_Booking_Fields_Email_Tag_Parser( $booking_id );
$parsed_message = $parser->parse( $parsed_message );
return $parsed_message;
}
add_filter( 'eventorganiser_email_template_tags', 'my_custom_email_tag', 10, 3 );
Why is this not in core?
While this is can be very useful for some, it’s not the most practical solution: If you have a development, staging and production environments it’s possible that the ID of the fields may be different in each one. As a result, you would need to change the ID given in %booking_field{X}%
appropriately in each environment – not exactly ideal.
Worse still, if you are building a site for a client, you need to tell that client not to remove the field. If they remove the field with ID 12, then %booking_field{12}%
would have to be removed/updated as well. If the client isn’t aware of these things, or not comfortable making any such changes, then you may end up creating work for yourself.
In short the reason this isn’t in core is because we’re holding out for something better, and more user (and developer) friendly (and we’re looking at options). But for the time being, you are more than welcome to make use of the above snippet.
As with all snippets on this site, the best place to use it is in a site utility plug-in (although it will work in functions.php as well). See Where should I put code from the tutorials?.
The post Including custom fields in confirmation e-mail appeared first on Event Organiser.