Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created May 13, 2024 09:51
Show Gist options
  • Save ipokkel/a0a64a502d67512ad62f543ec6d91d41 to your computer and use it in GitHub Desktop.
Save ipokkel/a0a64a502d67512ad62f543ec6d91d41 to your computer and use it in GitHub Desktop.
Set which user fields are added as "Extra Fields" to the admin checkout confirmation email. Fields not specified will be removed from the list added to the email.
<?php
/**
* Set which user fields are added as "Extra Fields" to the admin checkout confirmation 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/
*/
/**
* Add user fields to confirmation email.
*/
function my_pmpro_add_user_fields_to_email( $email ) {
global $wpdb;
// Set the names of the fields you want to include in the admin email
$admin_email_fields = array( 'pet_name', 'number_of_pets' );
//only update admin confirmation emails
if ( ! empty( $email ) && strpos( $email->template, 'checkout' ) !== false && strpos( $email->template, 'admin' ) !== false && ! empty( $email->data['membership_id'] ) ) {
//get the user_id from the email
$user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_email = %s LIMIT 1", $email->data['user_email'] ) );
if ( ! empty( $user_id ) ) {
//get user fields
$fields = my_pmpro_get_user_fields_for_user( $user_id );
// filter fields to only include the ones we want
$fields = array_filter(
$fields,
function( $field ) use ( $admin_email_fields ) {
return in_array( $field->meta_key, $admin_email_fields );
}
);
// add to bottom of email
if ( ! empty( $fields ) ) {
$email->body .= '<p>' . __( 'Extra Fields:', 'paid-memberships-pro' ) . '<br />';
foreach ( $fields as $field ) {
if ( ! pmpro_is_field( $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>';
} else {
$email->body .= '<p>' . __( 'No extra fields.', 'paid-memberships-pro' ) . '</p>';
}
}
}
return $email;
}
function swop_pmpro_add_user_fields_to_email_filters() {
if ( function_exists( 'my_pmpro_get_user_fields_for_user' ) && function_exists( 'my_pmpro_add_user_fields_to_email' ) ) {
remove_filter( 'pmpro_email_filter', 'pmpro_add_user_fields_to_email', 10, 2 );
add_filter( 'pmpro_email_filter', 'my_pmpro_add_user_fields_to_email', 10, 2 );
}
}
add_action( 'init', 'swop_pmpro_add_user_fields_to_email_filters' );
// get fields for a user's checkout level
function my_pmpro_get_user_fields_for_user( $user_id, $withlocations = false ) {
global $pmpro_user_fields;
$user_fields = array();
if ( ! empty( $pmpro_user_fields ) ) {
//cycle through groups
foreach ( $pmpro_user_fields as $where => $fields ) {
//cycle through fields
foreach ( $fields as $field ) {
if ( ! pmpro_is_field( $field ) ) {
continue;
}
if ( ! pmpro_check_field_for_level( $field, 'profile', $user_id ) ) {
continue;
}
if ( $withlocations ) {
$user_fields[ $where ][] = $field;
} else {
$user_fields[] = $field;
}
}
}
}
return $user_fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment