We have an event which has multiple dates – How can we create an ics file for each of these events so users who book tickets for this certain date can download the correct ics file?
Kelton Turvey
The plug-in can generate an ics file for the whole series of an individual event, but not individual dates of an event.
If you’re familiar with the iCal specification you could build a string with the details and then attach that as a string attachment. It depends on whether you want to provide a URL which points to an ics feed or whether you’re happy to attach it directly to the email.
To do the latter is a bit involved because WordPress’ wp_mail()
only supports attaching files, not strings generated at runtime .However you can use the following code:
<?php
class Myprefix_iCal_Email_Attacher{
protected $booking_id = false;
function setup( $attachments, $booking_id ){
$this->booking_id = $booking_id;
add_action( 'phpmailer_init', array( $this, 'attach_ical' ), 10 );
add_action( 'phpmailer_init', array( $this, 'cleanup' ), 11 );
return $attachments;
}
function attach_ical( $phpmailer ){
if( $this->booking_id ){
$event_id = eo_get_booking_meta( $this->booking_id , 'event_id' );
$occurrence_id = eo_get_booking_meta( $this->booking_id , 'occurrence_id' );
$title = esc_attr( get_the_title( $event_id ) );
$filename = sanitize_file_name( $title.'-'.$this->booking_id.'.ics' );
$ical_string = $this->get_ical( $event_id, $occurrence_id );
$phpmailer->AddStringAttachment( $ical_string, $filename );
$this->booking_id = false;
}
}
function get_ical( $event_id, $occurrence_id ){
return "...ics string feed for this event date...";
}
function cleanup(){
remove_action( 'phpmailer_init', array( $this, 'attach_ical' ), 10 );
}
}
$ical_attacher = new Myprefix_iCal_Email_Attacher();
add_filter( 'eventorganiser_booking_confirmed_email_attachments', array( $ical_attacher, 'setup' ), 10, 2 );
All you would need to implement is the get_ical()
method.
Stephen Harris