Last active
January 26, 2023 13:00
-
-
Save ipokkel/f2525f540cc7e5bf88afc2cc83278d68 to your computer and use it in GitHub Desktop.
Send a custom checkout email template with a custom subject line per membership level #pmpro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe sets a custom checkout email template and subject line per level. | |
* | |
* In this example recipe we created a custom email template for level ID's 1 & 2 | |
* in the "email" folder inside your customization plugin's directory. | |
* | |
* Note: This recipe and the required email template folders need to be added | |
* to a custom plugin and will not work from a third-party plugin that adds | |
* php customization recipes to the site. | |
* | |
* You can add this recipe to your site by creating a custom plugin. | |
* 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_custom_checkout_template_per_level( $email ) { | |
// Is this a checkout email for the member? | |
if ( strpos( $email->template, 'checkout' ) !== false && strpos( $email->template, 'admin' ) === false ) { | |
// which level is this email for? | |
$level_for_email = $email->data['membership_id']; | |
switch ( $level_for_email ) { | |
case '1': | |
$email->subject = 'Custom subject line for level 1'; | |
// Be sure to create the checkout_level_1.html email in the file location here. | |
$email->body = file_get_contents( dirname( __FILE__ ) . '/email/checkout_level_1.html' ); | |
break; | |
case '2': | |
$email->subject = 'Custom subject line for level 2'; | |
// Be sure to create the checkout_level_2.html email in the file location here. | |
$email->body = file_get_contents( dirname( __FILE__ ) . '/email/checkout_level_2.html' ); | |
break; | |
} | |
} | |
// Let's check for any remaining email variables and replace them with the data. | |
if ( is_array( $email->data ) ) { | |
foreach ( $email->data as $key => $value ) { | |
if ( 'body' != $key ) { | |
$email->body = str_replace( '!!' . $key . '!!', $value, $email->body ); | |
$email->subject = str_replace( '!!' . $key . '!!', $value, $email->subject ); | |
} | |
} | |
} | |
return $email; | |
} | |
// Set filter at a later priority than 10 | |
add_filter( 'pmpro_email_filter', 'my_pmpro_email_custom_checkout_template_per_level', 20, 2 ); |
For custom membership expiring reminder per level see: https://gist.github.com/ipokkel/311725d603a36d4fbe45244e4932e690
For a custom cancel email template per level see: https://gist.github.com/ipokkel/9054d94a7d7ab45084c188bf250c7fe3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For custom membership expired templates per level see: https://gist.github.com/ipokkel/2563d3e795a5cb208b7f8c09b693d294