Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active May 1, 2023 20:11
Show Gist options
  • Save ipokkel/48a772d2bcf3d915725c3c26ae237d63 to your computer and use it in GitHub Desktop.
Save ipokkel/48a772d2bcf3d915725c3c26ae237d63 to your computer and use it in GitHub Desktop.
Send a copy of invoice email to an additional email address if the member has set an addtional email address.
<?php
/**
* Send a copy of an Invoice email i a member has specified and additional invoice email addresses.
*
* This recipe require an additional email address to be set in usermeta table.
*
* Collecting an additional invoice email address should be achievable with a custom User Field.
* @link https://www.paidmembershipspro.com/documentation/user-fields/
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_email_bcc_invoice_per_member( $headers, $email ) {
$invoice_email_meta_key = 'additional_invoice_email';
if ( strpos( $email->template, 'invoice' ) !== false ) {
// Get the user
$user = get_user_by( 'email', $email->data['user_email'] );
// Get the additional invoice email address from the usermeta table.
$additional_email = get_user_meta( $user->ID, $invoice_email_meta_key, true );
// check if additional invoice email is set and is a valid email address
if ( $additional_email && is_email( $additional_email ) ) {
$headers[] = 'BCC: ' . $additional_email;
}
}
return $headers;
}
add_filter( 'pmpro_email_headers', 'my_pmpro_email_bcc_invoice_per_member', 10, 2 );
/**
* Send the Paid Memberships Pro invoice email on initial checkout for members.
*/
function pmpro_after_checkout_send_invoice_email( $user_id, $order ) {
// get user object by user id
$user = get_user_by( 'ID', $user_id );
if ( ! empty( $user ) ) {
$email = new PMProEmail();
$email->sendInvoiceEmail( $user, $order );
}
}
add_action( 'pmpro_after_checkout', 'pmpro_after_checkout_send_invoice_email', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment