april-s
3/13/2016 - 4:17 PM

Send email when invoice post is created. This uses a hook specific to the Advanced Custom Field plugin.

Send email when invoice post is created. This uses a hook specific to the Advanced Custom Field plugin.

<?php //* Mind this opening PHP tag

/**
 *	Send an email to the invoice_client_email custom field in the Invoice post type
 * 	The acf/save_post hook is specific to the Advanced Custom Fields plugin
 * 
 *	@author     Ren Ventura
 *	@link       http://www.engagewp.com/create-invoices-gravty-forms-wordpress
 */

//* Send a notice to the user when CPT is created
add_action( 'acf/save_post', 'send_invoice_notice', 20 );
function send_invoice_notice( $post_id ) {

    if ( 'invoice' == get_post_type( $post_id ) ) {
		
		// Get client's first name
		$name = get_field( 'invoice_client_name', $post_id );
		$first_name = explode( ' ', $name );

		// Get client's email
		$to = get_field( 'invoice_client_email', $post_id );

		// Get invoice link with client's email passed as query string
		$permalink = trailingslashit( get_permalink( $post_id ) ) . '?client_email=' . $to;

		// Set email subject
		$subject = 'Your EngageWP Service Invoice';

		// Set email message
		$message = 'Hi, ' . $first_name[0] . '. An invoice was just created and assigned to you on EngageWP. Click here to view invoice details: ' . $permalink;

		// Send email
		wp_mail( $to, $subject, $message );

    }

}