Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strangerstudios/d68d830d394b03d68ee9335a2085794b to your computer and use it in GitHub Desktop.
Save strangerstudios/d68d830d394b03d68ee9335a2085794b to your computer and use it in GitHub Desktop.
Use a custom checkout_renewal.html email template for renewal purchases in Paid Memberships Pro
/*
Use a custom checkout_renewal.html email template for renewals.
Add this code to a custom plugin.
Make sure there is a folder /email/ in the plugin folder.
Add a file /email/checkout_renewal.html in that email folder.
*/
function my_pmpro_email_checkout_renewal($email) {
$replace_data_again = false;
//only filter emails with invoices
if(empty($email->data['invoice_id']))
return $email;
//get order
$order = new MemberOrder($email->data['invoice_id']);
//make sure we have a real order
if(empty($order) || empty($order->id))
return $email;
//check if there has been another order for the same user/level in the past
global $wpdb;
$is_renewal = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_membership_orders WHERE user_id = '" . $order->user_id . "' AND membership_id = '" . $order->membership_id . "' AND id <> '" . $order->id . "' AND status NOT IN('refunded', 'review', 'token', 'error') AND gateway_environment = '" . $order->gateway_environment . "' LIMIT 1");
if(!$is_renewal)
return $email;
//this is a renewal! let's do our stuff
//update subject
$email->subject = "Thank you for your application.";
//update body
$email->body = file_get_contents(dirname(__FILE__) . "/email/checkout_renewal.html");
//replace data
if(is_string($email->data))
$email->data = array("body"=>$email->data);
if(is_array($email->data)) {
foreach($email->data as $key => $value) {
$email->body = str_replace("!!" . $key . "!!", $value, $email->body);
}
}
return $email;
}
add_filter('pmpro_email_filter', 'my_pmpro_email_checkout_renewal');
@JarrydLong
Copy link

JarrydLong commented Feb 23, 2021

Note: Ensure that there is an email folder in the same folder that this code runs in. This is to ensure that line 36 directs to the correct file path. If you've added this recipe to a PMPro Customizations plugin, your path to the checkout_renewal.html template should lead to pmpro-customizations/emails/checkout_renewal.html

@kimwhite
Copy link

When using with the Email Templates order you will need to add a higher load order number.
EXAMPLE
add_filter('pmpro_email_filter', 'my_pmpro_email_checkout_renewal', 20);

@laurenhagan0306
Copy link

This recipe is included in the blog post on "Customize the Membership Checkout Confirmation Email for Membership Renewals" at Paid Memberships Pro here: https://www.paidmembershipspro.com/customize-membership-checkout-confirmation-email-membership-renewals/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment