Support

Home Forums Ticketing Add-on Include ticket image in email [solution]

Include ticket image in email [solution]

Posted: June 30, 2016 at 9:13 am


cv

June 30, 2016 at 9:13 am

This is a technical solution and quite advanced stuff. My hope is that EE will either consider adding this functionality to the Ticketing Add-on or make a fuller article on the subject.

We wanted to include a jpeg image of the ticket in our confirmation emails and so had to roll our own solution. Here is how we did it.

Requires:
Experience of EE and writing functions in PHP
EE Ticketing Add-on
Account with web2image provider http://www.convertapi.com/
Understanding of Rest or something to help like https://github.com/educoder/pest

Problem:
No means in EE of putting an image of ticket in the emails.
No means in EE of attaching ticket to an email.
The ticket is rendered in the browser and QR code is rendered by JavaScript (no way of grabbing it separately)

Solution:
We opted for a workaround to trying to recreate the elements of the ticket and used a web2image converter to convert the entire web page of the ticket into a jpeg. The benefit of this is that you can maintain the same control over ticket designs per event etc and we simply swoop in and grab an exact copy of the ticket.

1.Create a custom shortcode to use in a message template (ours based on this code) https://gist.github.com/Pebblo/e87cc8e30c4848dcdfe2
2. Create a custom function to be called by the shortcode to do processing and helper functions for paths and naming.
3. Call the image2web service passing the URL to the ticket.
4. Receive the image of the ticket back and save to the uploads directory (or similar)
5. Return the link to the ticket image in the shortcode to create an image in the email.

The below code is what we used to solve this. It’s

    not

going to work if you copy and paste it as its missing our context and it has some variables that need setting, but you should be able to work this out if you met my above requirements 😉 This meets our needs but of course there are lots of improvements to be made.

The service also does web2pdf but we you only be able to link to the pdf right now as EE doesn’t have a hook for attachments (yet-please!)

Hope this helps
M

===========================================================

// shortcode to add the ticket to the email message
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'register_new_ticket_shortcodes', 10, 2 );
function register_new_ticket_shortcodes( $shortcodes, EE_Shortcodes $lib ) {
	if ( $lib instanceof EE_Event_List_Shortcodes ) {
		$shortcodes['[TICKETS_SHORTCODE]'] = _('Ticket custom shortcode');
	}
	return $shortcodes;
}

// required for the ticket shortcode
add_filter( 'FHEE__EE_Shortcodes__parser_after', 'register_new_ticket_shortcodes_parser', 10, 5 );
function register_new_ticket_shortcodes_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) {
	global $bridge;
		if ( $shortcode == '[TICKETS_SHORTCODE]' ) {
		 $bridge->createImageFromService(site_url().'/?ee=ee-txn-tickets-url&token='.$data->reg_obj->reg_url_link(),$data->reg_obj->reg_url_link());
			return '<div class="column-wrap" ><img class="eticket" src="'.$bridge->getTicketPath($data->reg_obj->reg_url_link()).'" alt="your ticket" /></div>';
		}
	return $parsed;
}

	/**
	 * uses an external service to convert a dynamically created page to an image
	 * @param string $url
	 * @param string $token
	 */
	public function createImageFromService($url,$token) {
		$ticket_jpg = $this->getTicketName($token);
		if(!file_exists($ticket_jpg)){
		try {
			$API = new Pest($this->apiAddress);
			$data=array('ApiKey'=>$this->apiKey,'CUrl'=>$url,'PageWidth'=>810);
			$image= $API->post('/Web2Image/', $data);
			file_put_contents($ticket_jpg, $image);
			} catch (Exception $e) {
				$this->writeToLog('ERROR - '.$e->getMessage());
			}	
		
		}
	
	}
	/**
	 * returns the path to an image (ticket)
	 * @param string $token
	 * @return string
	 */
	public function getTicketPath($token){
		return site_url().'/wp-content/uploads/tickets/ticket_'.$token.'.jpg';
	}
	
	private function getTicketName($token){
		$upload_dir = wp_upload_dir();
		$user_dirname = $upload_dir['basedir'].'/tickets';
		if ( ! file_exists( $user_dirname ) ) {
			wp_mkdir_p( $user_dirname );
		}
		return $user_dirname.'/ticket_'.$token.'.jpg';
	}


Josh

  • Support Staff

July 1, 2016 at 8:27 am

Thanks for taking the time to leave this valuable feedback. I’ve registered your suggestion and we will keep it in mind for future features and updates!

The support post ‘Include ticket image in email [solution]’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso