Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaryOJob/c9fedf42a532f8b4069e3227970034be to your computer and use it in GitHub Desktop.
Save MaryOJob/c9fedf42a532f8b4069e3227970034be to your computer and use it in GitHub Desktop.
Add Register Helper fields to the admin checkout notification email.
<?php
/**
* Add Custom Registration Fields and their values to the Admin Checkout Notification Email.
*
* 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_pmprorh_add_extra_fields_to_admin_email( $email ) {
global $wpdb;
//only update admin confirmation emails
if ( ! empty( $email ) && strpos( $email->template, 'checkout' ) !== false && strpos( $email->template, 'admin' ) !== false ) {
//get the user_id from the email
$user_id = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . $email->data['user_email'] . "' LIMIT 1" );
if ( ! empty( $user_id ) ) {
//get meta fields
global $pmprorh_registration_fields;
$profile_fields = array();
if ( ! empty( $pmprorh_registration_fields ) ) {
//cycle through groups
foreach ( $pmprorh_registration_fields as $where => $fields ) {
//cycle through fields
foreach ( $fields as $field ) {
if ( ! is_a( $field, 'PMProRH_Field' ) || ! pmprorh_checkFieldForLevel( $field, 'profile', $user_id ) ) {
continue;
}
if ( ! empty( $field->profile ) ) {
$profile_fields[] = $field;
}
}
}
}
unset( $fields );
$fields = $profile_fields;
//add to bottom of email
if ( ! empty( $fields ) ) {
$email->body .= '<p>Extra Fields (Admin) :<br />';
foreach ( $fields as $field ) {
if ( ! is_a( $field, 'PMProRH_Field' ) ) {
continue;
}
$email->body .= '- ' . $field->label . ': ';
$value = get_user_meta( $user_id, $field->meta_key, true );
if ( $field->type == 'file' && is_array( $value ) && ! empty( $value['fullurl'] ) ) {
$email->body .= $value['fullurl'];
} elseif ( is_array( $value ) ) {
$email->body .= implode( ', ', $value );
} else {
$email->body .= $value;
}
$email->body .= '<br />';
}
$email->body .= '</p>';
}
}
}
return $email;
}
function swop_pmprorh_pmpro_email_filter_extra_fields() {
// Let's only do this if Register Helper is active
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return;
}
remove_filter( 'pmpro_email_filter', 'pmprorh_pmpro_email_filter' );
add_filter( 'pmpro_email_filter', 'my_pmprorh_add_extra_fields_to_admin_email', 10, 2 );
}
add_action( 'init', 'swop_pmprorh_pmpro_email_filter_extra_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment