Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created May 20, 2024 18:40
Show Gist options
  • Save ipokkel/4cfed246d3e5805024d5c78cf95da1d3 to your computer and use it in GitHub Desktop.
Save ipokkel/4cfed246d3e5805024d5c78cf95da1d3 to your computer and use it in GitHub Desktop.
Add Invoice ID, Date, Total, and URL email variables to PMPro emails if they have a membership ID.
<?php
/**
* Add invoice_id, invoice_date, and invoice_total to PMPro email data.
*
* 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_add_invoice_email_variables( $data, $email ) {
// Let's only do this if required.
if ( ( isset( $data['invoice_id'] ) && isset( $data['invoice_date'] ) && isset( $data['invoice_total'] ) && isset( $data['invoice_url'] ) ) || ! isset( $data['membership_id'] ) || empty( $data['membership_id'] ) ) {
return $data;
}
// Get the user.
$user = get_user_by( 'email', $email->email );
// Get the membership level.
$membership_level = $data['membership_id'];
// Get last order for level.
$invoice = new MemberOrder();
$invoice->getLastMemberOrder( $user->ID, 'success', $membership_level );
// Add invoice_id, invoice_date, and invoice_total to email data
$data['invoice_id'] = $invoice->code;
$data['invoice_date'] = date_i18n( get_option( 'date_format' ), $invoice->timestamp );
$data['invoice_total'] = pmpro_formatPrice( $invoice->total );
$data['invoice_url'] = pmpro_login_url( pmpro_url( 'invoice', '?invoice=' . $invoice->code ) );
return $data;
}
add_filter( 'pmpro_email_data', 'my_pmpro_add_invoice_email_variables', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment